【发布时间】:2011-08-15 00:57:48
【问题描述】:
我在后面的代码中有这个 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 项目中不起作用?
标签: c# .net wpf data-binding binding