WPF variable binding / DependencyProperty

I play with WPF binding and variables. Apparently, you can only bind DependencyProperties. I came up with the following that works great: File with code:

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:

 <Window x:Class="WpfApplication3.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase" Title="MainWindow" Height="350" Width="525" DataContext="{Binding RelativeSource={RelativeSource Self}}"> <Grid> <TextBox Height="31" HorizontalAlignment="Left" Margin="84,86,0,0" Name="textBox1" VerticalAlignment="Top" Width="152" Text="{Binding Test, Mode=TwoWay, diag:PresentationTraceSources.TraceLevel=High}"/> <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="320,85,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /> <TextBox Height="31" HorizontalAlignment="Left" Margin="84,138,0,0" Name="textBox2" Text="{Binding Test, Mode=TwoWay}" VerticalAlignment="Top" Width="152" /> </Grid> 

Two text blocks update one another. And the button sets them to "AAA".

But now I have replaced the Setter function with what is commented out (simulating some manipulations with this value). I would expect that whenever the property value is changed, it will be reset to "BBB". It does this when you click the button, that is, when you set the property in code. But for some reason, this does not affect WPF bindings, that is, you can change the contents of the TextBox and, therefore, the property, but, apparently, Setter is never called. I wonder why this is so, and how the expected behavior could be achieved.

+6
c # wpf binding xaml dependency-properties
source share
2 answers

The CLR property wrapper for the Dependency property will never be guaranteed, and therefore you should never place any additional logic there. Whenever you need additional logic when changing DP, you should use the modified callback property.

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 } 
+13
source share

Your change will not affect the binding, because XAML will call SetValue directly, instead of invoking its own set of properties. This is how the dependency property system works. When a dependency property is registered, you can specify a default value. This value is returned from GetValue and is the default value for your property.

Check out the link below and read Robert Rossney's post for a fair overview.

WPF: what distinguishes a dependency property from a regular CLR property?

also do not miss

http://msdn.microsoft.com/en-us/library/ms753358.aspx

and

http://msdn.microsoft.com/en-us/library/ms752914.aspx

Also note that unlike common CLR properties, any custom logic that you write to the setter will not be executed in the dependency properties, you should use the PropertyChangedCallback mechanism instead

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

+2
source share

All Articles