触发PropertyChanged时,WPF依赖项属性设置器不会触发,但源值不会更改

时间:2010-04-28 09:38:21

标签: c# wpf data-binding dependency-properties

我的自定义文本框上有一个int依赖项属性,它包含一个支持值。它绑定到一个int? DataContext上的属性。

如果我在DataContext中引发PropertyChanged事件,并且源属性的值未更改(保持为null),则不会触发依赖项属性的setter。

这是一个问题,因为我想更新PropertyChanged上的自定义文本框(清除文本),即使源属性保持不变。但是,我没有找到任何符合我想要的绑定选项(有一个UpdateSourceTrigger属性,但我想在这里更新目标,而不是源代码)。 也许有更好的方法来通知Textbox它需要清除它的文本,我愿意接受任何建议。

来源,根据要求(简化)

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");
      }
  }

自定义文本框(目标):

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 个答案:

答案 0 :(得分:16)

XAML将直接调用SetValue,而不是调用属性设置器。我不记得具体细节,但我刚才遇到了类似的问题。您不应该在Value的setter中放置任何逻辑,而是在Dependency Property更改时定义回调,并从那里更新值。

相关问题