C# 激发PropertyChanged但未更改源值时,WPF依赖项属性设置程序未激发
我的自定义文本框上有一个int依赖属性,它包含一个支持值。它绑定到int?属性 如果我在DataContext中引发PropertyChanged事件,并且源属性的值没有更改(保持为null),那么依赖项属性的setter不会被激发 这是一个问题,因为我想更新PropertyChanged上的自定义文本框(清除文本),即使源属性保持不变。但是,我没有找到任何符合我要求的绑定选项(有一个UpdateSourceTrigger属性,但我想在这里更新目标,而不是源)。 也许有更好的方法通知文本框它需要清除文本,我愿意接受任何建议 来源,按要求(简化) 数据上下文(源): 自定义文本框(目标):C# 激发PropertyChanged但未更改源值时,WPF依赖项属性设置程序未激发,c#,wpf,data-binding,dependency-properties,C#,Wpf,Data Binding,Dependency Properties,我的自定义文本框上有一个int依赖属性,它包含一个支持值。它绑定到int?属性 如果我在DataContext中引发PropertyChanged事件,并且源属性的值没有更改(保持为null),那么依赖项属性的setter不会被激发 这是一个问题,因为我想更新PropertyChanged上的自定义文本框(清除文本),即使源属性保持不变。但是,我没有找到任何符合我要求的绑定选项(有一个UpdateSourceTrigger属性,但我想在这里更新目标,而不是源)。 也许有更好的方法通知文本框它需要
XAML将直接调用SetValue,而不是调用您的属性设置器。我记不清具体细节了,但不久前我遇到了类似的问题。您不应该在setter中为Value添加任何逻辑,而是应该在依赖项属性更改时定义回调,并从中更新值。您可以发布一些代码吗?这会让你更容易理解你的问题。如何在DataContext中引发PropertyChanged事件?即使源相同,它也应该更新。发布你的代码,解决方案应该很容易发现。我发布了一些代码。应用程序太复杂,无法在此处包含整个源代码。名字被替换了,太好了!我在寻找为什么我在片场的逻辑没有被执行。
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");
}
}
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
}