[英]Binding to a property of a property in WPF
我對WPF還是很陌生,這真的讓我陷入困境。 我試圖綁定到自定義UserControl上的成員的成員:
控件:
<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;
}
...
}
ESMTest類
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
。 如果您刪除,則綁定應該可以正常工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.