当PropertyChanged被触发时,WPF依赖属性设置器不触发,但是源值没有改变 [英] WPF dependency property setter not firing when PropertyChanged is fired, but source value is not changed

查看:1585
本文介绍了当PropertyChanged被触发时,WPF依赖属性设置器不触发,但是源值没有改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的自定义文本框中有一个int依赖属性,它保存一个后备值。它绑定到一个int? DataContext上的属性。



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

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



根据要求简单)



DataContext(source):

  private int? _foo 

public int? Foo
{
get
{
//绑定正在工作,因为_foo被检索(在此处命中一个断点)。
// RaisePropertyChanged(Foo)从别处调用,即使_foo的值没有被更改
return _foo;
}
set
{
//断点被用户输入,所以绑定正在工作
_foo = value;
RaisePropertyChanged(Foo);
}
}

自定义文本框(目标):

  public double?值
{
get
{
return(double?)GetValue(ValueProperty);
}
set
{
//当Foo为空且Value也为空时,断点不会在此处
SetValue(ValueProperty,value);

//这是当值设置为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)
{
//当Foo为空且Value也为空时,断点不会在此处
}


解决方案

XAML将直接调用SetValue,而不是调用你的财产安置者。我不记得具体细节,但是我之前遇到过类似的问题。您不应该在Value的setter中放置任何逻辑,而是在依赖属性更改时定义回调,并从中更新值。


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 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.

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.

这篇关于当PropertyChanged被触发时,WPF依赖属性设置器不触发,但是源值没有改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆