代码之家  ›  专栏  ›  技术社区  ›  Jake Pearson

WPF复合控件

  •  6
  • Jake Pearson  · 技术社区  · 15 年前

    我试图在WPF中创建一个具有标签和文本框的可重用用户控件。我想将属性添加到我的用户控件中,以便将两个子控件的文本字段冒泡到父控件中,以便轻松绑定。我读到我需要在DependencyProperties中添加所有者,这是一个小骗局。这是我的密码。看起来很近,但不太对。有什么想法吗?

    以下是XAML:

    <UserControl x:Class="MAAD.AircraftExit.Visual.LabelTextBox"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="20" Width="300">
        <DockPanel>
            <TextBlock Text="{Binding Path=Label, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" DockPanel.Dock="Left" TextAlignment="Right" Width="122" />
            <TextBlock Text=": " DockPanel.Dock="Left"/>
            <TextBox Text="{Binding Path=Text, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />
        </DockPanel>
    </UserControl>
    

    以及背后的代码:

    public partial class LabelTextBox : UserControl
    {
        public static readonly DependencyProperty LabelProperty = DependencyProperty.Register("Label", typeof(string), typeof(LabelTextBox));
        public string Label
        {
            get { return (string)GetValue(LabelProperty); }
            set { SetValue(LabelProperty, value); }
        }
    
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(LabelTextBox));
        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(LabelTextBox.TextProperty, value); }
        }
    
        public LabelTextBox()
        {
            InitializeComponent();
    
            ClearValue(HeightProperty);
            ClearValue(WidthProperty);
        }
    }
    

    编辑:这是最终的工作代码。我切换到相对源绑定。

    2 回复  |  直到 13 年前
        1
  •  6
  •   David Rogers    15 年前

    绑定实际上是一种方式:

    XAML:

    <UserControl x:Class="testapp.LabelTextBox "
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300" x:Name="This">
    <DockPanel>
        <TextBlock DockPanel.Dock="Left" TextAlignment="Right" Width="70" Name="label" Text="{Binding Label, ElementName=This}"  />
        <TextBlock Text=": " DockPanel.Dock="Left" />
        <TextBox Name="textBox" Text="{Binding Text, ElementName=This}" />
    </DockPanel>
    

    代码落后:

        public partial class LabelTextBox : UserControl
    {
        public LabelTextBox()
        {
            InitializeComponent();
            Label = "Label";
            Text = "Text";
        }
        public static readonly DependencyProperty LabelProperty = DependencyProperty.Register("Label", typeof(string), typeof(LabelTextBox), new FrameworkPropertyMetadata(LabelPropertyChangedCallback));
        private static void LabelPropertyChangedCallback(DependencyObject controlInstance, DependencyPropertyChangedEventArgs args)
        {
        }
        public string Label
        {
            get { return (string) GetValue(LabelProperty); }
            set { SetValue(LabelProperty, value); }
        }
    
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(LabelTextBox), new FrameworkPropertyMetadata(TextPropertyChangedCallback));
        private static void TextPropertyChangedCallback(DependencyObject controlInstance, DependencyPropertyChangedEventArgs args)
        {
        }
        public string Text
        {
            get { return (string) GetValue(TextProperty); }
            set { SetValue(LabelTextBox.TextProperty, value); }
        }
    }
    
        2
  •  1
  •   Tim Cooper    13 年前

    我还没有研究您的实现为什么不起作用,但我真的不理解您为什么这样做。为什么不定义用户控件上所需的依赖属性,然后绑定到它们?

    public static readonly DependencyProperty LabelTextProperty = ...;
    

    然后在XAML中:

    <Label Content="{Binding LabelText}"/>