繁体   English   中英

如何在XAML中绑定UIElements?

[英]How to bind UIElements in XAML?

我有一节课:

class LinkedTextBox: TextBox
{
    public TextBox TextBoxA { get; set; }
    public TextBox TextBoxB { get; set; }
}

说我有两个文本框:

    <TextBox x:Name="txt1" />
    <TextBox x:Name="txt2" />

如何在我的Xaml上指定TextBoxes?

我的测试:

(1)“ TypeBox的TypeConverter不支持从字符串转换。

    <local:LinkedTextBox TextBoxA="txt1" TextBoxB="txt2" />

(2)“ A'Binding'不能在'LinkedTextBox'类型的'TextBoxA'属性上设置。'Binding'只能在DependencyObject的DependencyProperty上设置。

    <local:LinkedTextBox 
        TextBoxA="{Binding ElementName=txt1}"  
        TextBoxB="{Binding ElementName=txt2}"  
        />

我认为有一种明显的方法可做,但我不知道如何......

对。 你的第二个例子是正确的XAML,但它失败了,因为TextBoxATextBoxB属于错误的属性。 Binding的目标必须是DependencyPropertyDependencyObject ,就像在锡上所说的那样。 TextBox已经是一个DependencyObject ,你正在对它进行子类化,因此该部分需要处理。 定义DependencyProperty是微不足道的。

您需要定义TextBoxA这个样子,和TextBoxB同样:

public class LinkedTextBox : TextBox
{
    #region TextBoxA Property
    public TextBox TextBoxA
    {
        get { return (TextBox)GetValue(TextBoxAProperty); }
        set { SetValue(TextBoxAProperty, value); }
    }

    //  Careful with the parameters you pass to Register() here.
    public static readonly DependencyProperty TextBoxAProperty =
        DependencyProperty.Register("TextBoxA", typeof(TextBox), typeof(LinkedTextBox),
            new PropertyMetadata(null));
    #endregion TextBoxA Property
}

但你的意图是什么? 你想达到什么目的? 你很可能通过以正常方式将现有属性彼此绑定来实现它,而没有任何这些子类monkeyshines。 可能你想要一个附加属性 ,这是一种特殊类型的依赖属性。

UPDATE

OP希望添加说明文本框之间关系的视觉元素。 如果你想添加一个视觉叠加层,那么WPF的方法就是写一个Adorner 因此,您将使用TextBoxATextBoxB依赖项属性编写某种TextBoxLinkingAdorner ,并将其应用于主文本框,这取决于您的要求甚至可能不必是子类。

您的依赖属性可能需要在其值更改时执行一些操作; 如果是这样,假设一个名为TextBoxLinkerAdornerAdorner子类,它们看起来更像这样:

    #region TextBoxA Property
    public TextBox TextBoxA
    {
        get { return (TextBox)GetValue(TextBoxAProperty); }
        set { SetValue(TextBoxAProperty, value); }
    }


    public static readonly DependencyProperty TextBoxAProperty =
        DependencyProperty.Register("TextBoxA", typeof(TextBox), 
            typeof(TextBoxLinkerAdorner),
            new FrameworkPropertyMetadata(null,
                    FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                    TextBoxA_PropertyChanged)
                        { DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });

    protected static void TextBoxA_PropertyChanged(DependencyObject d, 
        DependencyPropertyChangedEventArgs e)
    {
        var obj = d as TextBoxLinkerAdorner;
    }
    #endregion TextBoxA Property

如果您在文本框中查看的是它们的大小和位置,您可以编写一个链接任意UIElements而不仅仅是文本框的装饰器。 天空是极限! 如果你能梦想它,你可以装饰它!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM