Why does ToggleButton fail to obey binding when clicked?
-
Please help. I tracked down a bug down to the fact that a toggle button in my app ignores it’s binding. I’ve simplified to the enclosed example WPF app. What I want is the BoundToChecked toggle button to only reflect the real value of ValueSource.Checked. What occurs is when the BoundToChecked toggle button is clicked it changes visual state even if ValueSource.AllowChange is false! Any suggestions or advice would be most appreciated. Thanks, Ron
// ValueSource.cs using System; using System.ComponentModel; namespace ToggleButtonBound { class ValueSource : INotifyPropertyChanged { private bool _allowChange; public bool AllowChange { get { return _allowChange; } set { _allowChange = value; OnPropertyChanged("AllowChange"); } } private bool _checked; public bool Checked { get {return _checked;} set { if (AllowChange) { _checked = value; OnPropertyChanged("Checked"); } } } #region INotifyPropertyChanged Implementation public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string property) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(property)); } } #endregion } } // Window1.xaml <Window x:Class="ToggleButtonBound.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:me="clr-namespace:ToggleButtonBound" Title="ToggleButtonBound" Height="300" Width="300"> <Window.Resources> <me:ValueSource x:Key="ValueSource" /> </Window.Resources> <StackPanel> <ToggleButton Content="Unbound"/> <ToggleButton Content="BoundToChecked" IsChecked="{Binding Source={StaticResource ValueSource}, Path=Checked, Mode=Default}"/> <CheckBox Content="AllowChange" IsChecked="{Binding Source={StaticResource ValueSource}, Path=AllowChange, Mode=Default}"/> </StackPanel> </Window>
-
Please help. I tracked down a bug down to the fact that a toggle button in my app ignores it’s binding. I’ve simplified to the enclosed example WPF app. What I want is the BoundToChecked toggle button to only reflect the real value of ValueSource.Checked. What occurs is when the BoundToChecked toggle button is clicked it changes visual state even if ValueSource.AllowChange is false! Any suggestions or advice would be most appreciated. Thanks, Ron
// ValueSource.cs using System; using System.ComponentModel; namespace ToggleButtonBound { class ValueSource : INotifyPropertyChanged { private bool _allowChange; public bool AllowChange { get { return _allowChange; } set { _allowChange = value; OnPropertyChanged("AllowChange"); } } private bool _checked; public bool Checked { get {return _checked;} set { if (AllowChange) { _checked = value; OnPropertyChanged("Checked"); } } } #region INotifyPropertyChanged Implementation public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string property) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(property)); } } #endregion } } // Window1.xaml <Window x:Class="ToggleButtonBound.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:me="clr-namespace:ToggleButtonBound" Title="ToggleButtonBound" Height="300" Width="300"> <Window.Resources> <me:ValueSource x:Key="ValueSource" /> </Window.Resources> <StackPanel> <ToggleButton Content="Unbound"/> <ToggleButton Content="BoundToChecked" IsChecked="{Binding Source={StaticResource ValueSource}, Path=Checked, Mode=Default}"/> <CheckBox Content="AllowChange" IsChecked="{Binding Source={StaticResource ValueSource}, Path=AllowChange, Mode=Default}"/> </StackPanel> </Window>
See this thread for a similar issue, there is a workaround posted but in your case binding the ToggleButton's Enabled property to AllowChange might be better.