I have a user control that i am using inside a Data Template, this user control contains a TextBox which is binded with Value property(declared as a Dependency Property) of my user control. In data template i bind this Value property with my actual property say Name(also a Dependency Property). It works properly, i assign a value to Name property while loading and my TextBox displays it, i change the value from TextBox and it Updates my Name property also. Now the problem comes while i add a PropertyChangedEventHandler 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 TextBox of user control. Can anyone tell me why?
ADDED:
my user control: xaml
<UserControl x:Name="usercontrol">
<StackPanel>
<TextBlock ......./>
<TextBox Binding={Binding ElementName=usercontrol, Path=Value, Mode=TwoWay}/>
</StackPanel>
</UserControl>
In Code Behind Value is a Dependency Property
I am using this in my DataTemplate
<HierarchicalDataTemplate DataType="{x:Type myLib:myClass}" ItemsSource="{Binding Children}">
<Expander Header="{Binding}">
<WrapPanel>
<edproperty:TextPropertyEditor Caption="Name" Value="{Binding Name, Mode=TwoWay}"/>
<edproperty:TextPropertyEditor .................../>
<edproperty:TextPropertyEditor .................../>
<edproperty:TextPropertyEditor ...................../>
</WrapPanel>
</Expander>
</HierarchicalDataTemplate>
This template i am using in a treeview, inside treeview i enter value in the textBox which updates the Value property of my usercontrol, that in turn updates Name property of myClass i am assigning a PropertyChangedCallback to my Name Property in myClass, performing some validation and reassigning the oldValue if the validation fails. it in turn updates Value property also, but i am still seeing that the textbox has the same value that i entered earlier not the updated one.
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 && !IsValid(e.NewValue.ToString()))
{
checkRequired=false;
(sender as myClass).Name = args.OldValue.ToString();
}
}
private static bool IsValid(string name)
{
.............some logic here
}