WPF 绑定不起作用

发布于 2024-10-03 20:21:56 字数 1634 浏览 1 评论 0原文

我很确定我正在做一些严重错误的事情,但无法弄清楚。

我围绕一个类创建了一个简单的包装器,并添加了一个依赖属性,以便我可以绑定到它。然而,绑定没有给出错误,但什么也不做。

为了简化事情,我将类更改为 TextBox,并得到了相同的结果。

public class TextEditor : TextBox
{
    #region Public Properties

    #region EditorText
    /// <summary>
    /// Gets or sets the text of the editor
    /// </summary>
    public string EditorText
    {
      get
      {
        return (string)GetValue(EditorTextProperty);
      }

      set
      {
        //if (ValidateEditorText(value) == false) return;
        if (EditorText != value)
        {
          SetValue(EditorTextProperty, value);
          base.Text = value;

          //if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("EditorText"));
        }
      }
    }

    public static readonly DependencyProperty EditorTextProperty =
        DependencyProperty.Register("EditorText", typeof(string), typeof(TextEditor));
    #endregion

    #endregion

    #region Constructors

    public TextEditor()
    {
      //Attach to the text changed event
      //TextChanged += new EventHandler(TextEditor_TextChanged);
    }

    #endregion

    #region Event Handlers

    private void TextEditor_TextChanged(object sender, EventArgs e)
    {
      EditorText = base.Text;
    }

    #endregion
}

当我运行以下 XAML 时,第一个给出结果,但第二个 (EditorText) 甚至没有命中 EditorText 属性。

<local:TextEditor IsReadOnly="True" Text="{Binding Path=RuleValue, Mode=TwoWay}" WordWrap="True" />
<local:TextEditor IsReadOnly="True" EditorText="{Binding Path=RuleValue, Mode=TwoWay}" WordWrap="True" />

I am pretty sure I am doing something dreadfully wrong, but can't figure it out.

I created a simple wrapper around a class and added a dependency property so I could bind to it. However, the binding gives no errors, but does nothing.

In order to simplify things I changed the class to TextBox, and got the same results.

public class TextEditor : TextBox
{
    #region Public Properties

    #region EditorText
    /// <summary>
    /// Gets or sets the text of the editor
    /// </summary>
    public string EditorText
    {
      get
      {
        return (string)GetValue(EditorTextProperty);
      }

      set
      {
        //if (ValidateEditorText(value) == false) return;
        if (EditorText != value)
        {
          SetValue(EditorTextProperty, value);
          base.Text = value;

          //if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("EditorText"));
        }
      }
    }

    public static readonly DependencyProperty EditorTextProperty =
        DependencyProperty.Register("EditorText", typeof(string), typeof(TextEditor));
    #endregion

    #endregion

    #region Constructors

    public TextEditor()
    {
      //Attach to the text changed event
      //TextChanged += new EventHandler(TextEditor_TextChanged);
    }

    #endregion

    #region Event Handlers

    private void TextEditor_TextChanged(object sender, EventArgs e)
    {
      EditorText = base.Text;
    }

    #endregion
}

When I run the following XAML the first gives results, but the second one (EditorText) doesn't even hit the EditorText property.

<local:TextEditor IsReadOnly="True" Text="{Binding Path=RuleValue, Mode=TwoWay}" WordWrap="True" />
<local:TextEditor IsReadOnly="True" EditorText="{Binding Path=RuleValue, Mode=TwoWay}" WordWrap="True" />

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

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

发布评论

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

评论(1

黎夕旧梦 2024-10-10 20:21:56

您正在 CLR 属性中做额外的工作。无法保证 WPF 将使用您的 CLR 属性,因此您不应该这样做。相反,在 DP 上使用元数据可以达到相同的效果。

public string EditorText
{
  get { return (string)GetValue(EditorTextProperty); }
  set { SetValue(EditorTextProperty, value); }
}

public static readonly DependencyProperty EditorTextProperty =
    DependencyProperty.Register(
        "EditorText",
        typeof(string),
        typeof(TextEditor),
        new FrameworkPropertyMetadata(OnEditorTextChanged));

private static void OnEditorTextChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
    var textEditor = dependencyObject as TextEditor;

    // do your extraneous work here
}

You're doing extra work in your CLR property. There is no guarantee that your CLR property will be used by WPF so you shouldn't be doing this. Instead, use metadata on your DP to achieve the same effect.

public string EditorText
{
  get { return (string)GetValue(EditorTextProperty); }
  set { SetValue(EditorTextProperty, value); }
}

public static readonly DependencyProperty EditorTextProperty =
    DependencyProperty.Register(
        "EditorText",
        typeof(string),
        typeof(TextEditor),
        new FrameworkPropertyMetadata(OnEditorTextChanged));

private static void OnEditorTextChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
    var textEditor = dependencyObject as TextEditor;

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