1
votes

I have working context menu for listview control in my WPF app. I'd like to have context menu items enabled if user right clicks on listview item but disabled them is click occurs on panel area of listview. Thanks MK

Update: this is the my list view that works but I'd like to disable MenuItems "Remove" and "Calculate" when user clicks on panel area. Thanks for responding

          <ListView Name="lb_proplist"  DisplayMemberPath ="Name" HorizontalAlignment="Left" ToolTip="Use right click to see more options"  
                  ItemsSource="{Binding  Converter={StaticResource FilterByPropTypeConverter}}" Margin="0,0,0,0" 
                  ContextMenuOpening="ContextMenu_ContextMenuOpening" >


          <ListView.ContextMenu>
            <ContextMenu >
              <MenuItem Name="cmi_addNew" Header="Add New"
                  Command="{Binding AddNewItemItem}"
                  CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}}" />
              <MenuItem Name="cmi_remove" Header="Remove"
                  Command="{Binding RemoveItem}"
                  CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, 
                  Path=PlacementTarget.SelectedItem}" />
              <MenuItem Name="cmi_calculate" Header="Calculate"
                  Command="{Binding CalculateItem}"
                  CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, 
                  Path=PlacementTarget.SelectedItem}" />

            </ContextMenu>
          </ListView.ContextMenu>
          <ListView.View>
            <GridView>
              <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="200"/>
            </GridView>
          </ListView.View>
        </ListView>
1
Can you post your code which you have tried but not working?Rohit Vats

1 Answers

3
votes

Well, this can be done using ItemsTemplate property for ListView:

<ListView.ItemTemplate>
    <DataTemplate>
        <DockPanel Tag = "{Binding DataContext, ElementName=myListView}">

            <TextBlock Text="{Binding}"/>

            <DockPanel.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Local Item 1" 
                              Command="{Binding Path=PlacementTarget.Tag.CommandName, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"/>
                    <MenuItem Header="Local Item 2"/>
                </ContextMenu>
            </DockPanel.ContextMenu>

        </DockPanel>
    </DataTemplate>
</ListView.ItemTemplate>

In example, ContextMenu is created only for items, but not for whole control.
Also, Tag is added into DockPanel to access original DataContext.


Well, in case of GridView you need to override style for ListViewItem, and bind ContextMenu as StaticResource.

                <ListView.Resources>
                    <ContextMenu x:Key="ItemContextMenu">
                        <MenuItem Header="Add New"/>
                    </ContextMenu>
                </ListView.Resources>
                <ListView.ItemContainerStyle>
                    <Style TargetType="{x:Type ListViewItem}">
                        <Setter Property="ContextMenu" Value="{StaticResource ItemContextMenu}"/>
                    </Style>
                </ListView.ItemContainerStyle>

Please, take a look at this article and answer.