简体   繁体   中英

WPF ContextMenu IsChecked Binding

How to write Binding for "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".

If I click "Hello2", I want to add check on "Hello2", and remove Check of "Hello1".

I tried for 1 week. but I can not. Please help me "How to write binding" There is no example for internet. I recently started WPF programming. 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. but C# does not allow me to use it...

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. You should also set the IsCheckable property to true if you want to be able to check the menu items. Also, you cannot set the binding mode to 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 :

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));
    }
}

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