[英]Simple WPF Binding not working
I'm learning WPF, and I'm stuck with Data Bindings. 我正在学习WPF,而且我坚持使用Data Bindings。 I have a TreeView, which ItemSource
is set to a ObserveableCollection<UIBrowserItem>
. 我有一个TreeView, ItemSource
设置为ObserveableCollection<UIBrowserItem>
。
My Binding looks like: 我的绑定看起来像:
<TreeView>
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="Header" Value="{Binding Path=Title}"/>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
And my UIBrowserItem
is very basic: 我的UIBrowserItem
非常基本:
public class UIBrowserItem
{
public string Title = "Test";
}
But the Items in the TreeView won't have a header set .. 但是TreeView中的Items没有标题集..
If you need further information, tell me 如果您需要更多信息,请告诉我
You can only bind to public properties , you have a public field. 您只能绑定到公共属性 ,您有一个公共字段。 Your code should be: 你的代码应该是:
public class UIBrowserItem
{
private String title = "Test";
public string Title
{
get { return title; }
set { title = value; }
}
If the title can change at run time, you also need to implement INotifyPropertyChanged
. 如果标题可以在运行时更改,则还需要实现INotifyPropertyChanged
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.