元素的数据文本需要设置。
XAML:
<Window x:Class="WpfDependencyPropertyTest.Window1" x:Name="mywindow"> <StackPanel> <Label Content="{Binding Path=Test,ElementName=mywindow}" /> </StackPanel> </Window>
C#:
public static readonly DependencyProperty TestProperty = DependencyProperty.Register("Test",typeof(string),typeof(Window1),new FrameworkPropertyMetadata("Test")); public string Test { get { return (string)this.GetValue(Window1.TestProperty); } set { this.SetValue(Window1.TestProperty,value); } }
还看到这个相关的问题: