简体   繁体   中英

How do I add a contextmenu to my listviewitems in WPF?

So I've been trying to add a context menu to my listviewitems but I cant figure it out. I managed to figure out how to do it for the while lsitview control but I cant seem to figure out how to do it for the actual listviewitems.

I did this

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

To my listviewcontrol which added the contextmenu to it but thats not what I want, i want it for the 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>

Option #1:

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

Option #2

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM