简体   繁体   中英

How to Bind IsChecked Property of Menu item

Here My code to bind IsChecked property of menu.

<MenuItem Header="_View">
                <MenuItem  IsCheckable="True" IsChecked="{Binding ElementName=TermMenu, Path=IsChecked}" Header="Term" />
                <MenuItem  IsCheckable="True" IsChecked="True" Header="Key"  />
                <MenuItem  IsCheckable="True" IsChecked="True" Header="Hand"  />
                <MenuItem  IsCheckable="True" IsChecked="True" Header="Rule"  />
            </MenuItem>

Here is the context menu

<ContextMenu x:Key="DataGridColumnHeaderContextMenu" >
                    <MenuItem x:Name="TermMenu" IsCheckable="True" IsChecked="True" Header="Key Term"  />
                    <MenuItem x:Name="Key" IsCheckable="True" IsChecked="True" Header="Key Term Description"  />
                    <MenuItem x:Name="ShortHand" IsCheckable="True" IsChecked="True" Header="Hand"  />
                    <MenuItem x:Name="Rule" IsCheckable="True" IsChecked="True" Header="Rule"  />

                </ContextMenu>

Now I want when the context menu item is check the main menu View will be checked automatically. But my code is not working. Please explain me why.

x:Name does not work in resources. the element with the specified name in ElementPath does not exist. this is why the binding doesn't work. you can't access TermMenu from the code behind either. You should bind both to a view-model as @wilford sugested.

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