2010-01-29 80 views
0

我想一些属性从我绑定代码隐藏.xaml.cs一些XAML代码,就像这样:WPF绑定问题 - 不起作用?

<TextBlock [someProperties] ... Text="{Binding ElementName=awesome, Path=value}" /> 
<TextBlock [someProperties] ... Text="{Binding Path=legendary}" /> 

在相关.xaml.cs文件我也有属性:

public String legendary = "High five"; 
public CoolObject awesome = new CoolObject(); //has a public property "String value = '5';" 

但我的TextBlocks只是不想显示该死的“高五”和“5”。我错过了什么?

回答

0

<TextBlock [someProperties] ... Text="{Binding ElementName=awesome, Path=value}" />不起作用。当你想绑定到可视树属性中的某些元素时,使用ElementName。 你需要

<TextBlock [someProperties] ... Text="{Binding Path=awesome.value}" /> 

而且你需要设置的TextBlock的DataContext属性来bject包含您需要绑定属性。

+0

感谢,设置在DataContext加上设置属性属性,而不是字段,做到了。好极了! – Thomas 2010-01-29 14:35:14

+0

不客气 – Yurec 2010-01-29 15:01:34

0

问题是“传奇”和“真棒”被声明为字段而不是属性。 WPF绑定不适用于字段。

0

你需要用属性包装你的领域。绑定不支持字段。

所以:

public String _legendary = "High five"; 
public String legendary { 
    get { return _legendary; } 
    set { _legendary = value; } 
} 

另外,如果你可能要考虑实施INotifyPropertyChanged的,以确保无论你结合你的财产得到更新当您更改属性的值。

+0

还不行..都宣称传说中的那样跟随,只是为了尝试: 公共字符串传说中的{{返回“五高”;}集合{}} – Thomas 2010-01-29 14:03:34