无法识别 WPF 依赖属性

发布于 2024-08-09 02:41:08 字数 1835 浏览 1 评论 0原文

我正在尝试克服不允许我绑定到常规 clr 属性的限制。

我使用的解决方案使用自定义依赖项属性,这些属性又会更改 clr 属性。

这是代码,

class BindableTextBox : TextBox
{
    public static readonly DependencyProperty BoundSelectionStartProperty = DependencyProperty.Register("BoundSelctionStart", typeof(int), typeof(BindableTextBox),
                                                                                                          new PropertyMetadata(new PropertyChangedCallback(BindableTextBox.onBoundSelectionStartChanged)));

    private static void onBoundSelectionStartChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((TextBox)d).SelectionStart = (int)e.NewValue;
    }

    private static readonly DependencyProperty BoundSelectionLenghtProperty = DependencyProperty.Register("BoundSelectionLenght", typeof(int), typeof(BindableTextBox),
                                                                                                            new PropertyMetadata(new PropertyChangedCallback(BindableTextBox.onBoundSelectionLenghtChanged)));

    private static void onBoundSelectionLenghtChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((TextBox)d).SelectionLength = (int)e.NewValue;
    }

    public int BoundSelectionStart
    {
        get { return (int)GetValue(BoundSelectionStartProperty); }
        set { SetValue(BoundSelectionStartProperty, value); }
    }

    public int BoundSelectionLenght
    {
        get { return (int)GetValue(BoundSelectionLenghtProperty); }
        set { SetValue(BoundSelectionLenghtProperty, value); }
    }
}

但是当我尝试将某些内容绑定到 BoundSelectionStart 时,它说我只能绑定到 DP。

<bindable:BindableTextBox Text="{Binding Name}" BoundSelectionStart="{Binding ElementName=slider1, Path=Value}" />

问题是什么?

I'm trying to overcome a limitation that doesn't allow me to bind to regular clr properties.

The solution I use uses custom dependency properties that in turn changes clr properties.

Here is the code

class BindableTextBox : TextBox
{
    public static readonly DependencyProperty BoundSelectionStartProperty = DependencyProperty.Register("BoundSelctionStart", typeof(int), typeof(BindableTextBox),
                                                                                                          new PropertyMetadata(new PropertyChangedCallback(BindableTextBox.onBoundSelectionStartChanged)));

    private static void onBoundSelectionStartChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((TextBox)d).SelectionStart = (int)e.NewValue;
    }

    private static readonly DependencyProperty BoundSelectionLenghtProperty = DependencyProperty.Register("BoundSelectionLenght", typeof(int), typeof(BindableTextBox),
                                                                                                            new PropertyMetadata(new PropertyChangedCallback(BindableTextBox.onBoundSelectionLenghtChanged)));

    private static void onBoundSelectionLenghtChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((TextBox)d).SelectionLength = (int)e.NewValue;
    }

    public int BoundSelectionStart
    {
        get { return (int)GetValue(BoundSelectionStartProperty); }
        set { SetValue(BoundSelectionStartProperty, value); }
    }

    public int BoundSelectionLenght
    {
        get { return (int)GetValue(BoundSelectionLenghtProperty); }
        set { SetValue(BoundSelectionLenghtProperty, value); }
    }
}

But when I try to bound something to BoundSelectionStart it says it says that I can only bind to DP.

<bindable:BindableTextBox Text="{Binding Name}" BoundSelectionStart="{Binding ElementName=slider1, Path=Value}" />

What is the problem?

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

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

发布评论

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

评论(1

猥琐帝 2024-08-16 02:41:08

您在该行中有一个拼写错误:

public static readonly DependencyProperty BoundSelectionStartProperty = DependencyProperty.Register(...)

第一个参数应该是“BoundSelectionStart”(Selection 中的 2x e),而不是“BoundSelctionStart”。

You have a typo in the line:

public static readonly DependencyProperty BoundSelectionStartProperty = DependencyProperty.Register(...)

The first parameter should be "BoundSelectionStart" (2x e in Selection), not "BoundSelctionStart".

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