在 WPF 中绑定到 属性 的 属性
Binding to a property of a property in WPF
我是 WPF 的新手,它真的让我陷入困境。我正在尝试绑定到自定义用户控件上的成员中的成员:
对照:
<UserControl
...
DataContext="{Binding RelativeSource={RelativeSource Self}}"
>
<Grid x:Name="TestControlRoot" HorizontalAlignment="Stretch" DataContext="TestControl" Margin="8">
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<Label Name="TitleLabel" Content="{Binding Path=esmTest.testName, Mode=OneWay}" HorizontalAlignment="Left" Foreground="White"/>
</StackPanel>
</Grid>
</UserControl>
控件的隐藏代码:
public partial class TestControl : UserControl
{
public static readonly DependencyProperty esmTestProperty = DependencyProperty.Register("esmTest", typeof(ESMTest), typeof(TestControl));
public ESMTest esmTest
{
get { return (ESMTest)GetValue(esmTestProperty); }
set { SetValue(esmTestProperty, value); }
}
public TestControl(ESMTest theTest)
{
InitializeComponent();
esmTest = theTest;
}
...
}
ESM测试class
public class ESMTest : DependencyObject
{
...
public static readonly DependencyProperty testNameProperty = DependencyProperty.Register("testName", typeof(string), typeof(ESMTest));
public string testName
{
get { return (string)GetValue(testNameProperty); }
set { SetValue(testNameProperty, value); }
}
}
当我 运行 这样做时,我没有在标签中获得任何内容。我在这里错过了什么?
另外,我发现很难找到有关 WPF 的良好信息来源,尤其是数据绑定。我可以找到关于如何解决非常特殊问题的超级基础教程或堆栈交换帖子。我希望有一篇关于 WPF 中数据绑定和 DependencyObjects 本质的综合文章,这样我就可以全面了解这些东西的工作原理以及我可能使用它的许多不同方式。谁能推荐个好资源?
总结这一行的评论
<Grid ... DataContext="TestControl">
您用 字符串 TestControl 覆盖上面设置的 DataContext
。如果您删除您的绑定应该可以正常工作
我是 WPF 的新手,它真的让我陷入困境。我正在尝试绑定到自定义用户控件上的成员中的成员:
对照:
<UserControl
...
DataContext="{Binding RelativeSource={RelativeSource Self}}"
>
<Grid x:Name="TestControlRoot" HorizontalAlignment="Stretch" DataContext="TestControl" Margin="8">
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<Label Name="TitleLabel" Content="{Binding Path=esmTest.testName, Mode=OneWay}" HorizontalAlignment="Left" Foreground="White"/>
</StackPanel>
</Grid>
</UserControl>
控件的隐藏代码:
public partial class TestControl : UserControl
{
public static readonly DependencyProperty esmTestProperty = DependencyProperty.Register("esmTest", typeof(ESMTest), typeof(TestControl));
public ESMTest esmTest
{
get { return (ESMTest)GetValue(esmTestProperty); }
set { SetValue(esmTestProperty, value); }
}
public TestControl(ESMTest theTest)
{
InitializeComponent();
esmTest = theTest;
}
...
}
ESM测试class
public class ESMTest : DependencyObject
{
...
public static readonly DependencyProperty testNameProperty = DependencyProperty.Register("testName", typeof(string), typeof(ESMTest));
public string testName
{
get { return (string)GetValue(testNameProperty); }
set { SetValue(testNameProperty, value); }
}
}
当我 运行 这样做时,我没有在标签中获得任何内容。我在这里错过了什么?
另外,我发现很难找到有关 WPF 的良好信息来源,尤其是数据绑定。我可以找到关于如何解决非常特殊问题的超级基础教程或堆栈交换帖子。我希望有一篇关于 WPF 中数据绑定和 DependencyObjects 本质的综合文章,这样我就可以全面了解这些东西的工作原理以及我可能使用它的许多不同方式。谁能推荐个好资源?
总结这一行的评论
<Grid ... DataContext="TestControl">
您用 字符串 TestControl 覆盖上面设置的 DataContext
。如果您删除您的绑定应该可以正常工作