I am driving crazy with a custom Dependency Property. I already checked loads of threads here, but haven’t found any solution yet. What I want to do is to replace the value of the Property if the source provides a specific value (null for the given example). No matter what I try, the property value within the source remains null and is never updated.
Here is my Custom Control:
public class TextBoxEx : TextBox
{
public TextBoxEx()
{
TrueValue = 0;
this.TextChanged += (s, e) =>
{
TrueValue = Text.Length;
SetCurrentValue(MyPropertyProperty, TrueValue);
var x = BindingOperations.GetBindingExpression(this, MyPropertyProperty);
if (x != null)
{
x.UpdateSource();
}
};
}
public int? TrueValue { get; set; }
public int? MyProperty
{
get { return (int?)GetValue(MyPropertyProperty); }
set { SetValue(MyPropertyProperty, value); }
}
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.Register("MyProperty", typeof(int?), typeof(TextBoxEx), new PropertyMetadata(null, PropertyChangedCallback));
private static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue == null)
{
d.SetCurrentValue(MyPropertyProperty, (d as TextBoxEx).TrueValue);
}
}
}
Here is the DataContext i am Binding:
public class VM : INotifyPropertyChanged
{
private int? _Bar = null;
public int? Bar
{
get { return _Bar; }
set
{
_Bar = value;
OnPropertyChanged("Bar");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
My Binding looks like this:
<local:TextBoxEx MyProperty="{Binding Bar, UpdateSourceTrigger=PropertyChanged}"/>
Remember: I need a TwoWay binding, so OneWayToSource does not work for me.
Any idea what I am not getting in here?
You just need to set the binding to two-way and it will work. But as that should be the default you can register the property accordingly using the following metadata:
Getting the expression in the
TextChangedhandler and updating source manually is not required so i would remove that code.If you do not explicitly set a mode on a binding the default will be used, from the documentation: