The CLR Property wrapper for a Dependency Property is never guaranteed to be called and therefore, you should never place any additional logic there. Whenever you need additional logic when a DP is changed, you should use the property changed callback.
Up vote 4 down vote favorite share g+ share fb share tw.
I'm playing around with WPF Binding and variables. Apparently one can only bind DependencyProperties. I have come up with the following, which works perfectly fine: The code-behind file: public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } public string Test { get { return (string)this.
GetValue(TestProperty); } set { this. SetValue(TestProperty, value); } //set { this. SetValue(TestProperty, "BBB"); } } public static readonly DependencyProperty TestProperty = DependencyProperty.
Register( "Test", typeof(string), typeof(MainWindow), new PropertyMetadata("CCC")); private void button1_Click(object sender, RoutedEventArgs e) { MessageBox. Show(Test); Test = "AAA"; MessageBox. Show(Test); } } XAML: The two TextBoxes update one an other.
And the Button sets them to "AAA". But now I replaced the Setter function with the one that is commented out (simulating some manipulation of the given value). I would expect that whenever the property value is changed it will be reset to "BBB".
It does so when you press the button, that is when you set the property in code. But it does for some reason not affect the WPF Bindings, that is you can change the TextBox contents and thus the property, but apparently the Setter is never called. I wonder why that is so, and how one would go about to achive the expected behaviour.
C# wpf xaml binding dependency-properties link|improve this question edited Jan 1 '11 at 20:46Meleak23.2k41861 asked Dec 28 '10 at 16:38Peter276 75% accept rate.
The CLR Property wrapper for a Dependency Property is never guaranteed to be called and therefore, you should never place any additional logic there. Whenever you need additional logic when a DP is changed, you should use the property changed callback. In your case.. public string Test { get { return (string)this.
GetValue(TestProperty); } set { this. SetValue(TestProperty, value); } } public static readonly DependencyProperty TestProperty = DependencyProperty. Register("Test", typeof(string), typeof(MainWindow), new PropertyMetadata("CCC", TestPropertyChanged)); private static void TestPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e) { MainWindow mainWindow = source as MainWindow; string newValue = e.
NewValue as string; // Do additional logic }.
Your change will not affect the binding because the XAML will call SetValue directly, instead of calling your property setter. That is how the dependency property system works. When a dependency property is registered a default value can be specified.
This value is returned from GetValue and is the default value for your property. Check the link below and read through to Robert Rossney's post to get a fair overview WPF: What distinguishes a Dependency Property from a regular CLR Property? Also don't miss msdn.microsoft.com/en-us/library/ms75335... and msdn.microsoft.com/en-us/library/ms75291... Also note that unlike in normal CLR properties any custom logic you write in the setter will not be executed in Dependency Properties,instead you have to use the PropertyChangedCallback mechanism http://blogs.msdn.com/b/delay/archive/2010/03/23/do-one-thing-and-do-it-well-tip-the-clr-wrapper-for-a-dependencyproperty-should-do-its-job-and-nothing-more.aspx.
Your change will not affect the binding because the XAML will call SetValue directly, instead of calling your property setter. That is how the dependency property system works. When a dependency property is registered a default value can be specified.
This value is returned from GetValue and is the default value for your property.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.