gpt4 book ai didi

c# - 简单的 WPF 绑定(bind)不起作用

转载 作者:行者123 更新时间:2023-11-30 16:12:09 26 4
gpt4 key购买 nike

我正在学习 WPF,并且一直在使用数据绑定(bind)。我有一个 TreeView,它 ItemSource设置为 ObserveableCollection<UIBrowserItem> .

我的绑定(bind)看起来像:

<TreeView>
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="Header" Value="{Binding Path=Title}"/>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>

还有我的UIBrowserItem非常基础:

public class UIBrowserItem 
{
public string Title = "Test";
}

但是 TreeView 中的 Items 不会有标题集..

如果您需要更多信息,请告诉我

最佳答案

您只能绑定(bind)到公共(public)属性,您有一个公共(public)字段。您的代码应该是:

public class UIBrowserItem 
{
private String title = "Test";
public string Title
{
get { return title; }
set { title = value; }
}

如果标题可以在运行时改变,你还需要实现INotifyPropertyChanged

关于c# - 简单的 WPF 绑定(bind)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23415076/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com