I have an int dependency property on my custom Textbox, which holds a backing value. It is bound to an int? property on the DataContext.
If I raise the PropertyChanged event in my DataContext, and the source property's value is not changed (stays null), then the dependency property's setter is not fired.
This is a problem, because I want to update the custom Textbox (clear the text) on PropertyChanged, even if the source property stays the same. However, I didn't find any binding option that does what I want (there is an UpdateSourceTrigger property, but I want to update the target here, not the source).
Maybe there is a better way to inform the Textbox that it needs to clear its text, I'm open to any suggestions.
Source, as requested (simplified)
DataContext (source):
private int? _foo;
public int? Foo
{
get
{
// The binding is working, because _foo is retrieved (hits a breakpoint here).
// RaisePropertyChanged("Foo") is called from elsewhere, even if _foo's value is not changed
return _foo;
}
set
{
// Breakpoint is hit on user input, so the binding is working
_foo = value;
RaisePropertyChanged("Foo");
}
}
Custom Textbox (target):
public double? Value
{
get
{
return (double?)GetValue(ValueProperty);
}
set
{
// When Foo is null and Value is also null, the breakpoint is not hit here
SetValue(ValueProperty, value);
// This is the piece of code that needs to be run whenever Value is set to null
if (value == null && !String.IsNullOrEmpty(Text))
{
Text = String.Empty;
}
}
}
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double?), typeof(CustomTextbox), new PropertyMetadata(null, ValueChangedHandler));
private static void ValueChangedHandler(DependencyObject dependecyObject, DependencyPropertyChangedEventArgs e)
{
// When Foo is null and Value is also null, the breakpoint is not hit here
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…