WPF binding problem with specific property names

I came across the following problem. I have a checkbox, the IsChecked property IsChecked bound to the CLR property in my MainWindow class. Here is the source code.

Code behind ( MainWindow.xaml.cs ):

 namespace MenuItemBindingTest { public partial class MainWindow : Window, INotifyPropertyChanged { private bool m_backedVariable = false; public bool IsPressAndHoldEnabled { get { return this.m_backedVariable; } set { this.m_backedVariable = value; OnPropertyChanged("IsPressAndHoldEnabled"); MessageBox.Show("Item changed: " + this.m_backedVariable); } } public MainWindow() { InitializeComponent(); this.m_checkbox.DataContext = this; } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { if (this.PropertyChanged != null) { this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } } 

XAML Code ( MainWindow.xaml ):

 <Window x:Class="MenuItemBindingTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Binding Problem Test" Width="525" Height="350"> <DockPanel> <CheckBox x:Name="m_checkbox" IsChecked="{Binding IsPressAndHoldEnabled}" HorizontalAlignment="Center" VerticalAlignment="Center" Content="Is Press and Hold enabled"/> </DockPanel> </Window> 

Now the problem is that the installation attribute for the IsPressAndHoldEnabled property IsPressAndHoldEnabled never called (i.e. the message box is never displayed) when the user checks or unchecks the box. However, it works when I rename a property to something else - for example, IsPressAndHoldEnabled2 .

Now my question is: why can't I use IsPressAndHoldEnabled as the name for my property? Does this have anything to do with the Stylus.IsPressAndHoldEnabled existing property?

+4
source share
2 answers

Interesting. I have no answers, but I have workarounds:

Separates the IsPressAndHoldEnabled property from a separate ViewModel class, unless the class was obtained from FrameworkElement.

In addition, the transition from the usual property to the Dependency property in the same MainWindow class worked - the DP callback is triggered.

+1
source

Did you specify TwoWay as the binding mode? Although I think that CheckBox.IsChecked uses the TwoWay binding mode by default ...

I think you may have messed up your binding context so that it does not find the IsPressAndHoldEnabled property. WPF bindings fail silently - a royal pain if you ask me.

Ensure that the checkbox is indeed bound to this property and that the binding context is indeed an object of the MainWindodw class.

+1
source

Source: https://habr.com/ru/post/1344774/


All Articles