我在代码后台有这个Text
依赖属性:
public static DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(MainWindow),
new PropertyMetadata("Hello world"));
public string Text {
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
我想将标签的内容绑定到Text
属性,以便标签显示Text
属性的实际值,反之亦然。
<Label Content="{Binding ???}" />
我该怎么做?
我以前做过这个,但现在我不记得怎么做了 - 而且它非常简单。最简单的代码将被接受。
<Label Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type WpfApplication1:MainWindow}}, Path=Text}" />
,但它仍然无法工作...难道绑定在全新的WPF项目中不起作用吗?在其他方面都正常的情况下? - Rasto