【问题标题】:How do I add a contextmenu to my listviewitems in WPF?如何将上下文菜单添加到 WPF 中的列表视图项?
【发布时间】:2017-10-09 16:33:10
【问题描述】:

所以我一直在尝试向我的 listviewitems 添加上下文菜单,但我无法弄清楚。 我设法弄清楚如何为 while lsitview 控件执行此操作,但我似乎无法弄清楚如何为实际的 listviewitems 执行此操作。

我做了这个

<ContextMenu>
<MenuItem Header="Remove"/>
</ContextMenu>

到我的 listviewcontrol,它添加了上下文菜单,但这不是我想要的,我想要它用于 listviewitems。

<ListView Margin="10,36,520,10" ScrollViewer.HorizontalScrollBarVisibility="Hidden" BorderBrush="Black" Padding="-1, -1, 1, 0" Background="Transparent" BorderThickness="1.000001" Name="lvUsers"  Style="{DynamicResource ListViewStyle1}">
            <ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListViewItem}">
                    <Setter Property="Background" Value="Transparent" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ListViewItem}">
                                <Border
                         BorderBrush="Transparent"
                         BorderThickness="0"
                         Background="{TemplateBinding Background}">

                                    <GridViewRowPresenter HorizontalAlignment="Stretch" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Width="Auto" Margin="0" Content="{TemplateBinding Content}"/>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>

                    </Setter>

                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Foreground" Value="White" />
                            <Setter Property="Background" Value="#696969" />
                            <Setter Property="BorderBrush" Value="#696969" />
                            <Setter Property="BorderThickness" Value="0" />
                        </Trigger>

                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Foreground" Value="White" />
                            <Setter Property="Background" Value="#696969" />
                            <Setter Property="BorderBrush" Value="#696969" />
                            <Setter Property="BorderThickness" Value="0" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.View>
                <GridView ColumnHeaderContainerStyle="{StaticResource GridViewColumnHeaderStyle1}">
                    <GridViewColumn x:Name="GridViewColumnName" Header="Name" Width="165">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <Image x:Name="Image_GridViewColumnName" Width="16" Height="16" Source="C:\Users\developer\source\repos\PortforwardWPF\PortforwardWPF\Images\minecraft.png" />
                                    <Label Content="{Binding Username}" Visibility="Visible" />
                                </StackPanel>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>

            </ListView.View>
        </ListView>

【问题讨论】:

    标签: c# .net xml visual-studio xaml


    【解决方案1】:

    选项#1:

        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="ContextMenu">
                <Setter.Value>
                    <ContextMenu />
                </Setter.Value>
            </Setter>
            <Setter Property="Background" Value="Transparent" />
    

    选项#2

                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListViewItem}">
                        <Border
                            BorderBrush="Transparent"
                            BorderThickness="0"
                            Background="{TemplateBinding Background}">
                            <Border.ContextMenu>
                                <ContextMenu />
                            </Border.ContextMenu>
    

    【讨论】:

    • 哦!惊人的!非常感谢楼主!
    猜你喜欢
    • 2017-06-05
    • 1970-01-01
    • 1970-01-01
    • 2014-06-03
    • 1970-01-01
    • 2019-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多