Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF: TextBox Text is not updating
    text
    copied!<p>I have a user control that i am using inside a <code>DataTemplate</code>, this <code>UserControl</code> contains a <code>TextBox</code> which is binded with <em>Value</em> property(declared as a <code>DependencyProperty</code>) of my <code>UserControl</code>. In data template I bind this <em>Value</em> property with my actual property say <em>Name</em> (also a <code>DependencyProperty</code>). It works properly, I assign a value to <em>Name</em> property while loading and my <code>TextBox</code> displays it, i change the value from <code>TextBox</code> and it updates my <em>Name</em> property also. Now the problem comes while I add a <code>PropertyChangedEventHandler</code> in the dependency property, I check for the value if it is valid, do nothing if valid and assign old value if not valid. In case of value not valid, when I assign it that old value, property updates but it is not displaying updated value in my <code>TextBox</code> of <code>UserControl</code>. Can anyone tell me why?</p> <p><em>XAML</em> of my <code>UserControl</code>:</p> <pre><code>&lt;UserControl x:Name="usercontrol"&gt; &lt;StackPanel&gt; &lt;TextBlock ......./&gt; &lt;TextBox Binding={Binding ElementName=usercontrol, Path=Value, Mode=TwoWay}/&gt; &lt;/StackPanel&gt; &lt;/UserControl&gt; </code></pre> <p>In code behind <em>Value</em> is a <code>DependencyProperty</code></p> <p>I am using this in my <code>DataTemplate</code>:</p> <pre><code>&lt;HierarchicalDataTemplate DataType="{x:Type myLib:myClass}" ItemsSource="{Binding Children}"&gt; &lt;Expander Header="{Binding}"&gt; &lt;WrapPanel&gt; &lt;edproperty:TextPropertyEditor Caption="Name" Value="{Binding Name, Mode=TwoWay}"/&gt; &lt;edproperty:TextPropertyEditor .................../&gt; &lt;edproperty:TextPropertyEditor .................../&gt; &lt;edproperty:TextPropertyEditor ...................../&gt; &lt;/WrapPanel&gt; &lt;/Expander&gt; &lt;/HierarchicalDataTemplate&gt; </code></pre> <p>This template I am using in a <code>TreeView</code>. Inside <code>TreeView</code> I enter value in the <code>TextBox</code> which updates the <em>Value</em> property of my <code>UserControl</code>, that in turn updates <em>Name</em> property of <code>myClass</code> I am assigning a <code>PropertyChangedCallback</code> to my <em>Name</em> Property in <code>myClass</code>, performing some validation and reassigning the <code>OldValue</code> if the validation fails. It in turn updates <em>Value</em> property also, but i am still seeing that the <code>TextBox</code> has the same value that i entered earlier not the updated one. </p> <pre><code>public string Name { get { return (string)GetValue(NameProperty); } set { SetValue(NameProperty, value); } } // Using a DependencyProperty as the backing store for Name. This enables animation, styling, binding, etc... public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(myClass), new UIPropertyMetadata(null, OnNameChanged)); protected static void OnNameChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) { if(checkRequired &amp;&amp; !IsValid(e.NewValue.ToString())) { checkRequired=false; (sender as myClass).Name = args.OldValue.ToString(); } } private static bool IsValid(string name) { .............some logic here } </code></pre>
 

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