我创建了一个UserControl,其中包含一个名为CustomLabel的类型为String的依赖项属性。
该控件包含Label,它应显示CustomLabel属性的值。
我可以使用OnLabelPropertyChanged事件处理程序在代码中执行此操作:
public class MyControl : UserControl
{
public static readonly DependencyProperty LabelProperty = DependencyProperty.Register(
"Label",
typeof(String),
typeof(ProjectionControl),
new FrameworkPropertyMetadata("FLAT", OnLabelPropertyChanged));
private static void OnLabelPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs eventArgs)
{
((Label)FindName("myLabel")).Content = (string)GetValue("LabelProperty");
}
}
我知道在XAML中必须有更简单的方法,例如:
...
<Label Content="{Binding ...point to the Label property... }"/>
...
但我尝试了很多组合(RelativeSource / Pah,Source / Path,x:Reference,只是写属性名......)并且没有任何效果......
我是WinForms的专家,并且在某些时候学习WPF,但这些东西对我来说仍然是陌生的。