我在自定義文本框上有一個int依賴項屬性,該屬性包含一個備用值。它綁定到一個int? DataContext上的屬性。當PropertyChanged被觸發時,WPF依賴項屬性設置器未觸發,但源值未更改
如果我在DataContext中引發PropertyChanged事件,並且源屬性的值未更改(保留爲空),則不會觸發依賴項屬性的setter。
這是一個問題,因爲我想更新PropertyChanged上的自定義文本框(清除文本),即使源屬性保持不變。但是,我沒有找到任何我想要的綁定選項(有一個UpdateSourceTrigger屬性,但我想在此更新目標,而不是源)。 也許有一個更好的方式來通知文本框,它需要清除它的文本,我願意接受任何建議。
源,如請求的(簡化的)
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");
}
}
自定義文本框(目標):
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
}
你能發表一些代碼嗎?這會讓你更容易理解你的問題。你在DataContext中究竟如何提升PropertyChanged事件? – wpfwannabe 2010-04-28 09:45:57
即使源代碼相同,它也應該更新。發佈你的代碼,解決方案應該很容易被發現。 – 2010-04-28 09:54:29
我發佈了一些代碼。該應用程序太複雜,無法在此處包含整個源代碼。名稱已被替換。 – 2010-04-28 12:24:45