当使用IValueConverter检查另一个MenuItem时,WPF禁用MenuItem

时间:2021-03-13 19:43:31

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.

我在选中Item1时尝试启用Item2,并在未选中Item1时禁用它。如何使用IValueConverter将IsChecked属性转换为Boolean并将其绑定到Item2中的IsEnabled属性。

<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 个解决方案

#1


0  

You don't need converter, write following xaml:

你不需要转换器,写下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>

#1


0  

You don't need converter, write following xaml:

你不需要转换器,写下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>