C# WPF复合控件
我试图在WPF中创建一个可重用的UserControl,它有一个标签和一个文本框。我想向UserControl添加属性,以便将两个子控件的文本字段冒泡到父控件,以便轻松绑定。我读到,我需要通过将所有者添加到DependencyProperties中来增加一点技巧。这是我的代码。这似乎很接近,但并不完全正确。有什么想法吗 以下是Xaml:C# WPF复合控件,c#,wpf,data-binding,C#,Wpf,Data Binding,我试图在WPF中创建一个可重用的UserControl,它有一个标签和一个文本框。我想向UserControl添加属性,以便将两个子控件的文本字段冒泡到父控件,以便轻松绑定。我读到,我需要通过将所有者添加到DependencyProperties中来增加一点技巧。这是我的代码。这似乎很接近,但并不完全正确。有什么想法吗 以下是Xaml: <UserControl x:Class="MAAD.AircraftExit.Visual.LabelTextBox" xmlns="http:
<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>
编辑:这是最终的工作代码。我切换到了相对源代码绑定。我还没有详细了解您的实现为什么不起作用,但我真的不明白您为什么这样做。为什么不在UserControl上定义所需的依赖项属性,然后绑定到它们
public static readonly DependencyProperty LabelTextProperty = ...;
然后在XAML中:
<Label Content="{Binding LabelText}"/>
绑定确实是一种方式: XAML:
感谢您的代码,当我尝试使用它时,我得到了以下输出:System.Windows.Data错误:4:找不到引用为'ElementName=this'的绑定源。BindingExpression:Path=Label;DataItem=null;目标元素为“TextBlock”(名称=“”);目标属性为“Text”(类型为“String”)。对不起,我找到了。您将其定义为控件的名称。谢谢我一开始也试过同样的方法,假设这就是所有者的概念。不幸的是,我遇到了同样的问题,并最终求助于使用绑定。此外,我的非依赖属性还有一个不同的自定义解决方案,用于传播属性更改通知。
<Label Content="{Binding LabelText}"/>
<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); }
}
}