将转换器与 DependencyProperty 结合使用

发布于 2024-12-04 12:36:23 字数 890 浏览 1 评论 0原文

我在派生的 AutoCompleteBox 控件上创建了 DependencyProperty --> IsReadOnly

从那里,我尝试通过转换器设置值(T/F)。根据转换器值,我想更新 DependencyProperty 设置器中的嵌套 TextBox 样式。在 XAML 中显式设置属性 (IsReadOnly="True") 效果很好,并且设置器会触发并更新样式。但是,通过转换器执行此操作不会触发 DependencyProperty 的设置器。我似乎在此处粘贴代码片段时遇到问题(第一次海报)..所以我会尽力提供快速的代码运行:

Property on AutoCompleteBox:

IsReadOnly="{Binding Converter={StaticResource IsReadOnlyVerifier}, ConverterParameter= '编辑客户端'}"

调用转换器,转换器根据用户的权限返回 true 或 false。但是,这不会调用已注册 DependencyProperty 的 setter。

.. 放

        {
            if (value)
            {
                var style = StyleController.FindResource("ReadOnlyTextBox") as Style;
                TextBoxStyle = style;
            }
            else
            {
                TextBoxStyle = null;
            }
            SetValue(IsReadOnlyProperty, value);
        }

I've created a DependencyProperty on my derived AutoCompleteBox control --> IsReadOnly

From there, I'm trying to set the value (T/F) via a converter. Based on the converter value, I would like to update the nested TextBox style in the setter of the DependencyProperty. Explicitly setting the property in the XAML (IsReadOnly="True") works fine, and the setter fires and updates the style. However, doing this via the converter does NOT fire of the setter of the DependencyProperty. I seem to be having trouble pasting code snippets here (first time poster).. so I'll do my best to give an quick code run through:

Property on AutoCompleteBox:

IsReadOnly="{Binding Converter={StaticResource IsReadOnlyVerifier}, ConverterParameter='Edit Client'}"

Which calls out to the Converter, which returns either true or false based on the User's permissions. This however does not call the setter of the registered DependencyProperty.

..
set

        {
            if (value)
            {
                var style = StyleController.FindResource("ReadOnlyTextBox") as Style;
                TextBoxStyle = style;
            }
            else
            {
                TextBoxStyle = null;
            }
            SetValue(IsReadOnlyProperty, value);
        }

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

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

发布评论

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

评论(1

已下线请稍等 2024-12-11 12:36:23

这是一个经典的新手陷阱。绑定将直接使用 SetValue 设置目标 DependencyProperty,它们不会通过 POCO 属性设置器方法分配值。

您的 IsReadOnly 属性应如下所示:-

  #region public bool IsReadOnly
  public bool IsReadOnly
  {
       get { return (bool)GetValue(IsReadOnlyProperty); }
       set { SetValue(IsReadOnlyProperty, value); }
  }

  public static readonly DependencyProperty IsReadOnlyProperty =
     DependencyProperty.Register(
         "IsReadOnly",
         typeof(bool),
         typeof(MyAutoCompleteBox),
         new PropertyMetaData(false, OnIsReadOnlyPropertyChanged) );

  private static void OnIsReadOnlyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  {
       MyAutoCompleteBox source = d as MyAutoCompleteBox;
       source.OnIsReadOnlyChanged((bool)e.OldValue, (bool)e.NewValue);    
  }

  private void OnIsReadOnlyChanged(bool oldValue, bool newValue)
  {
       TextBoxStyle = newValue ? StyleControlller.FindResource("ReadOnlyTextBox") as Style ? null;
  }
  #endregion

设置依赖项属性时,它会影响任何其他更改,您应该向 PropertyMetaData 提供 PropertyChangedCallback 委托注册DependencyProperty时。每当使用 SetValue 为此属性分配值时都会调用此函数。

This is a classic newbie gotcha. Bindings will set the target DependencyProperty using SetValue directly, they don't assign a value via the POCO property setter method.

Your IsReadOnly property should look like this:-

  #region public bool IsReadOnly
  public bool IsReadOnly
  {
       get { return (bool)GetValue(IsReadOnlyProperty); }
       set { SetValue(IsReadOnlyProperty, value); }
  }

  public static readonly DependencyProperty IsReadOnlyProperty =
     DependencyProperty.Register(
         "IsReadOnly",
         typeof(bool),
         typeof(MyAutoCompleteBox),
         new PropertyMetaData(false, OnIsReadOnlyPropertyChanged) );

  private static void OnIsReadOnlyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  {
       MyAutoCompleteBox source = d as MyAutoCompleteBox;
       source.OnIsReadOnlyChanged((bool)e.OldValue, (bool)e.NewValue);    
  }

  private void OnIsReadOnlyChanged(bool oldValue, bool newValue)
  {
       TextBoxStyle = newValue ? StyleControlller.FindResource("ReadOnlyTextBox") as Style ? null;
  }
  #endregion

It affect any other changes when a dependency property is set you should supply a PropertyChangedCallback delegate to the PropertyMetaData when registering the DependencyProperty. This will be called whenever SetValue is used to assign a value for this property.

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