Disable/Enable context menu item depending on listview item

2.1k views Asked by At

I have a UserControl with a ListView in it. In addition I have a class listViewItems.cs with a DisplayMemberBinding to a GridView in listView.

Each ListView-Item has a context-menu. Now I'm trying to enable/disable the context-menu-items depending if a value in class ListViewItems is null

I've tried a binding to the IsEnabled property to the boolean value ShowResItemEn in class ListViewItems.cs but it does not work.

DataOutput.xaml

<ListView.Resources>
    <ContextMenu x:Name="cmListView" x:Key="ItemContextMenu" DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"> 
        <MenuItem x:Name="itmRes"
                    Header="Reservierungen anzeigen"
                    IsEnabled="{Binding PlacementTarget.SelectedItem.ShowResItemEn, RelativeSource={RelativeSource FindAncestor,AncestorType=ContextMenu}}"
                    Command="{Binding ShowResItemCmd}"
                    CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem}" >
        </MenuItem>
    </ContextMenu>
</ListView.Resources>

class ListViewItems.cs

public Boolean ShowResItemEn
{
   get
   {
       return (auftrNr[0] == null) ? false : true; 
   }
}
1

There are 1 answers

0
user3597422 On BEST ANSWER

Ok it works now. I've setted the AncestorType wrong

IsEnabled="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}},Path=SelectedItem.ShowBesItemEn}"