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

发布于 2024-09-25 19:24:41 字数 1355 浏览 3 评论 0原文

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

例如

<l:SelectControl StateType="A" Text="Hello"/>

,在这个示例中,我有一个名为 SelectControlUserControl,它有一个名为 StateType 的属性,它可以在其设置器中操作一些其他属性。

为了帮助说明问题,我在示例中添加了另一个名为 Text 的属性,请继续阅读,我将进一步解释。

代码隐藏摘录...

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

现在,如果我在 setter 上设置断点(对于 StateTypeText),结果发现它永远不会执行。

但是,为 Text 声明的值,即“Hello”出现在其数据绑定 TextBox 中,当然,我将另一个文本控件绑定到 StateType' s 的价值我也能看到。

有谁知道发生了什么事?

I have a WPF application where I'm using dependency properties in codebehind which I want to set via XAML declarations.

e.g.

<l:SelectControl StateType="A" Text="Hello"/>

So in this example I have a UserControl called SelectControl, which has a property called StateType which manipulate some other properties in it's setter.

To help illustrate the problem, I've put another property called Text in the example, read on and I'll explain further.

Codebehind excerpt...

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

Now, if I set a breakpoint on the setter (for either StateType or Text), it turns out it's never executed.

However values declared for Text, i.e. "Hello" appears in it's data bound TextBox, and of course it I bind another text control to StateType's value I can see that too.

Does anyone know what's happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人事已非 2024-10-02 19:24:41

依赖属性的“CLR 包装器”仅在通过代码完成时才会被调用。 XAML 取决于 DependencyProperty.Register(...) 调用中指定的名称。因此,不要像上面那样“扩展”依赖属性的 setter 逻辑,只需将自定义逻辑放在 PropertyChangedCallback 函数。

The "CLR-wrappers" for dependency properties only get called when done through code. XAML depends on the name specified in the DependencyProperty.Register(...) call. So, instead of "extending" the logic of the setter for your dependency property like you did above, just put your custom logic in a PropertyChangedCallback function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文