简体   繁体   English

WPF ContextMenu IsChecked绑定

[英]WPF ContextMenu IsChecked Binding

How to write Binding for "IsChecked" ? 如何为“ IsChecked”编写绑定? I want to check either of 2 menu. 我想检查两个菜单之一。

If I click "Hello1", I want to add check on "Hello1", and remove Check of "Hello2". 如果单击“ Hello1”,则要添加对“ Hello1”的检查,并删除“ Hello2”的检查。

If I click "Hello2", I want to add check on "Hello2", and remove Check of "Hello1". 如果单击“ Hello2”,则要添加对“ Hello2”的检查,并删除“ Hello1”的检查。

I tried for 1 week. 我尝试了1周。 but I can not. 但是我不能。 Please help me "How to write binding" There is no example for internet. 请帮助我“如何写绑定”没有互联网示例。 I recently started WPF programming. 我最近开始WPF编程。 I only can do form application. 我只能做表格申请。 it is very big different.... 这有很大的不同。

<Window x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <ContextMenu Name="ContextMenuMain" x:Key="menuListBox">
            <MenuItem Header="Hello1" IsChecked="{Binding Path=IsCheck1, Mode=OneWay}"  Name="ContextMenu_Hello1" Click="ContextMenuClick_Hello1"/>
        <MenuItem Header="Hello2"   IsChecked="{Binding Path=IsCheck2, Mode=OneWay}" Name="ContextMenu_Hello2" Click="ContextMenuClick_Hello2"/>
    </ContextMenu>
    </Window.Resources>

</Window>

My most difficult point is below code. 我最难的是在代码下面。 I tried to use name of "ContextMenu_Hello1" in MainWindow. 我试图在MainWindow中使用“ ContextMenu_Hello1”的名称。 but C# does not allow me to use it... 但是C#不允许我使用它...

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            ContextMenu_Hello1.DataContext  // error !
        }
    }

Is this out of scope ? 这超出范围了吗? but Why ? 但为什么 ?

Set the ContextMenu property of the window. 设置窗口的ContextMenu属性。 You should also set the IsCheckable property to true if you want to be able to check the menu items. 如果您希望能够检查菜单项,还应该将IsCheckable属性设置为true Also, you cannot set the binding mode to OneWay : 另外,您不能将绑定模式设置为OneWay

<Window x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">

    <Window.ContextMenu>
        <ContextMenu Name="ContextMenuMain">
            <MenuItem Header="Hello1" IsCheckable="True" IsChecked="{Binding Path=IsCheck1}"  Name="ContextMenu_Hello1" />
            <MenuItem Header="Hello2" IsCheckable="True" IsChecked="{Binding Path=IsCheck2}" Name="ContextMenu_Hello2" />
        </ContextMenu>
    </Window.ContextMenu>
</Window>

When you have made these changes, you can then bind to any property of the DataContext : 进行这些更改后,可以绑定到DataContext任何属性:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow ()
    {
        InitializeComponent();
        DataContext = this;
    }

    private bool _isCheck1;
    public bool IsCheck1
    {
        get { return _isCheck1; }
        set
        {
            _isCheck1 = value;
            NotifyPropertyChanged();
            _isCheck2 = !_isCheck1;
            NotifyPropertyChanged("IsCheck2");
        }
    }

    private bool _isCheck2;
    public bool IsCheck2
    {
        get { return _isCheck2; }
        set
        {
            _isCheck2 = value;
            NotifyPropertyChanged();
            _isCheck1 = !_isCheck2;
            NotifyPropertyChanged("IsCheck1");
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM