none
binding MenuItem IsChecked with dependency property RRS feed

  • Question

  • Hello, I'm new to C# and .NET and I cannot seem to get this binding to work with the MenuItem IsChecked property.  

    The desired behavior is a follows: when the menu item is clicked, the dependency property should be updated, and when the dependency property is changed programmatically, the menu item's isChecked should be updated to this value.  

     

    <MenuItem Header="Antialiasing" Name="antialiasing" IsCheckable="True" Click="update_Click" IsChecked="{Binding Path=Antialiasing, Mode=TwoWay}"/>
    
    

    public partial class MainWindow
    {
     public static DependencyProperty AntialiasingProperty;
    
     public bool Antialiasing
     {
      get { return (bool)GetValue(AntialiasingProperty); }
      set { SetValue(AntialiasingProperty, value); }
     }
     
     static MainWindow()
     {
      AntialiasingProperty =
       DependencyProperty.Register("Antialiasing", typeof(bool), typeof(MainWindow), new PropertyMetadata(true));
      }
     }
    

     


    thanks,

    alex


    Thursday, September 1, 2011 6:42 PM

Answers

  • Hi alex,

    please try this (without DependencyProperty):

    <Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Height="350"
        Width="525"
        x:Name="mainWnd">
    
    <MenuItem Header="Antialiasing" Name="antialiasing" IsCheckable="True" Click="update_Click" IsChecked="{Binding Path=Antialiasing, ElementName=mainWnd}"/>
    
    
    public partial class MainWindow : INotifyPropertyChanged
    {
     #region INotifyPropertyChanged Member
    
     public event PropertyChangedEventHandler PropertyChanged;
    
     #endregion
    
     public void InvokePropertyChanged(string propertyName)
     {
      var eh = this.PropertyChanged;
      if (eh != null)
      {
       eh(this, new PropertyChangedEventArgs(propertyName);
      }
     }
    
     public bool Antialiasing
     {
      get { return _antialiasing; }
      set
      {
       _antialiasing = value;
       InvokePropertyChanged("Antialiasing");
      }
     }
     
     static MainWindow()
     {
      // remove
     }
    
     public MainWindow()
     {
      InitializeComponent();
     }
    }
    
    
    



    "It's time to kick ass and chew bubble gum... and I'm all outta gum." - Duke Nukem
    • Marked as answer by _413X_ Thursday, September 1, 2011 8:50 PM
    Thursday, September 1, 2011 7:04 PM

All replies

  • Hi alex,

    please try this (without DependencyProperty):

    <Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Height="350"
        Width="525"
        x:Name="mainWnd">
    
    <MenuItem Header="Antialiasing" Name="antialiasing" IsCheckable="True" Click="update_Click" IsChecked="{Binding Path=Antialiasing, ElementName=mainWnd}"/>
    
    
    public partial class MainWindow : INotifyPropertyChanged
    {
     #region INotifyPropertyChanged Member
    
     public event PropertyChangedEventHandler PropertyChanged;
    
     #endregion
    
     public void InvokePropertyChanged(string propertyName)
     {
      var eh = this.PropertyChanged;
      if (eh != null)
      {
       eh(this, new PropertyChangedEventArgs(propertyName);
      }
     }
    
     public bool Antialiasing
     {
      get { return _antialiasing; }
      set
      {
       _antialiasing = value;
       InvokePropertyChanged("Antialiasing");
      }
     }
     
     static MainWindow()
     {
      // remove
     }
    
     public MainWindow()
     {
      InitializeComponent();
     }
    }
    
    
    



    "It's time to kick ass and chew bubble gum... and I'm all outta gum." - Duke Nukem
    • Marked as answer by _413X_ Thursday, September 1, 2011 8:50 PM
    Thursday, September 1, 2011 7:04 PM
  • That works, thank you very much!
    Thursday, September 1, 2011 8:50 PM