Note that there are some explanatory texts on larger screens.

plurals
  1. PODependency property re-entrancy (or: why does this work?)
    primarykey
    data
    text
    <p>Put simply, I can create 2 dependency properties in a WPF control and put code in each property change notification to change the other property (i.e <code>PropA</code> change sets <code>PropB</code> and <code>PropB</code> change sets <code>PropA</code>).</p> <p>I would expect this to disappear up its own backside but WPF seems to handle it nicely. That's actually very handy for my purposes but I can't find this behaviour documented anywhere.</p> <p>So what's going on? Does the WPF dependency property change notification system guard against reentrancy?</p> <p>Representative code follows:</p> <p><strong>XAML</strong>:</p> <pre><code>&lt;Window x:Class="WPFReentrancy1.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"&gt; &lt;Grid&gt; &lt;TextBox Text="{Binding PropB, UpdateSourceTrigger=PropertyChanged}"/&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p><strong>Code behind:</strong></p> <pre><code> public partial class MainWindow : Window { public string PropA { get { return (string)GetValue(PropAProperty); } set { SetValue(PropAProperty, value); } } public static readonly DependencyProperty PropAProperty = DependencyProperty.Register("PropA", typeof (string), typeof (MainWindow),new UIPropertyMetadata("0", PropAChanged)); public string PropB { get { return (string)GetValue(PropBProperty); } set { SetValue(PropBProperty, value); } } public static readonly DependencyProperty PropBProperty = DependencyProperty.Register("PropB", typeof (string), typeof (MainWindow), new UIPropertyMetadata("", PropBChanged)); private static void PropBChanged(DependencyObject lDependencyObject, DependencyPropertyChangedEventArgs lDependencyPropertyChangedEventArgs) { ((MainWindow) lDependencyObject).PropA = (string) lDependencyPropertyChangedEventArgs.NewValue; } private static void PropAChanged(DependencyObject lDependencyObject, DependencyPropertyChangedEventArgs lDependencyPropertyChangedEventArgs) { ((MainWindow) lDependencyObject).PropB = double.Parse((string) lDependencyPropertyChangedEventArgs.NewValue).ToString("0.000"); } public MainWindow() { InitializeComponent(); DataContext = this; PropA = "1.123"; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload