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.
我在自定义文本框上有一个int依赖项属性,它包含一个支持值。它绑定到一个int? 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.
如果我在DataContext中引发PropertyChanged事件,并且源属性的值未更改(保持为null),则不会触发依赖项属性的setter。
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.
这是一个问题,因为我想在PropertyChanged上更新自定义文本框(清除文本),即使源属性保持不变。但是,我没有找到任何符合我想要的绑定选项(有一个UpdateSourceTrigger属性,但我想在这里更新目标,而不是源代码)。也许有更好的方法来通知Textbox它需要清除它的文本,我对任何建议持开放态度。
Source, as requested (simplified)
来源,按要求(简化)
DataContext (source):
DataContext(来源):
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
}
1 个解决方案
#1
16
The XAML will call SetValue directly, instead of calling your property setter. I can't remember exactly the specifics, but I ran into a similar problem a while ago. You should not put any logic in the setter for Value, instead define a callback for when the Dependency Property changes, and update the value from there.
XAML将直接调用SetValue,而不是调用属性设置器。我不记得具体细节,但我刚才遇到了类似的问题。您不应在Value的setter中放置任何逻辑,而是在Dependency Property更改时定义回调,并从那里更新值。
#1
16
The XAML will call SetValue directly, instead of calling your property setter. I can't remember exactly the specifics, but I ran into a similar problem a while ago. You should not put any logic in the setter for Value, instead define a callback for when the Dependency Property changes, and update the value from there.
XAML将直接调用SetValue,而不是调用属性设置器。我不记得具体细节,但我刚才遇到了类似的问题。您不应在Value的setter中放置任何逻辑,而是在Dependency Property更改时定义回调,并从那里更新值。