开发者

WPF dependency property setter not firing when PropertyChanged is fired, but source value is not changed

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 tar开发者_如何学运维get 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
        }


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜