WPF:XAML属性声明没有通过Setters设置?

oco*_*odo 5 wpf setter xaml dependency-properties

我有一个WPF应用程序,我在代码隐藏中使用依赖属性,我想通过XAML声明设置.

例如

<l:SelectControl StateType="A" Text="Hello"/>
Run Code Online (Sandbox Code Playgroud)

所以在这个例子中我有一个UserControl被调用的SelectControl,它有一个属性StateType,在它的setter中操作一些其他的属性.

为了帮助说明问题,我Text在示例中调用了另一个属性,继续阅读,我将进一步解释.

Codebehind摘录......

public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(String), typeof(SelectControl));

public String Text
{
  get { return (String)GetValue(TextProperty); }
  set { SetValue(TextProperty, value); }
}

public static readonly DependencyProperty StateTypeProperty = DependencyProperty.Register("StateType", typeof(String), typeof(SelectControl));

public String StateType
{
  get { return (String)GetValue(StateTypeProperty) }
  set
    {
      switch (value)
      {
        case "A":
          AnotherPropertyBoolean = true;
          break;
        case "B":
          AnotherPropertyBoolean = false;
          break;
       default:
         // this is only an example... 
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

现在,如果我在setter上设置一个断点(对于其中一个StateType或者Text),事实证明它从未执行过.

但是声明的值Text,即"Hello"出现在它的数据绑定中TextBox,当然我将另一个文本控件绑定到其StateType值,我也可以看到它.

有谁知道发生了什么?

ASa*_*nch 14

只有在通过代码完成时才会调用依赖项属性的"CLR-wrappers".XAML依赖于DependencyProperty.Register(...)调用中指定的名称.因此,不要像上面那样"扩展"依赖属性的setter的逻辑,而只需将自定义逻辑放在PropertyChangedCallback函数中.