WPF Disable MenuItem when the other MenuItem is Checked using IValueConverter

3.6k views Asked by At

I tried to enable Item2 when Item1 is checked and disable it when Item1 is not checked. How to do that with IValueConverter to convert IsChecked property to Boolean and bind it to IsEnabled Property in Item2.

<ContextMenu x:Name="ItemsContxtMenu">
<MenuItem IsCheckable="True" x:Name="Item1" Header="item1 .."/>
<MenuItem x:Name="Item2" Header="item2 .." IsEnabled="{Binding ElementName=Item1, Path=IsChecked"}/>
</ContextMenu>
1

There are 1 answers

2
mohsen mousavi On

You don't need converter, write following xaml:

<Menu VerticalAlignment="Top">
    <MenuItem Header="Items">
        <MenuItem Name="item1" Header="Item #1" IsCheckable="True" />
        <MenuItem Name="item2" Header="Item #2"  IsEnabled="{Binding ElementName=item1,Path=IsChecked}" />
    </MenuItem>
</Menu>