WPF复合控件

Jak*_*son 6 c# data-binding wpf

我正在尝试在WPF中创建一个具有Label和TextBox的可重用UserControl.我想在我的UserControl中添加属性,将两个子控件的Text字段冒泡到父控件,以便轻松绑定.我读到我需要通过向DependencyProperties添加所有者来进行一些hocus pocus.这是我现在的代码.它似乎很接近但不太对劲.有任何想法吗?

这是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>
Run Code Online (Sandbox Code Playgroud)

而背后的代码:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Dav*_*ers 6

绑定真的是要走的路:

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>
Run Code Online (Sandbox Code Playgroud)

代码背后:

    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); }
    }
}
Run Code Online (Sandbox Code Playgroud)