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

标签 c# wpf binding model treeview

我正在学习 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/

相关文章:

c# - 用作另一个 IdentityServer 的外部身份提供者的 IdentityServer 无法正确重定向

c# - 使用 Tcplistener 的 HTTPS

wpf - ComboBox 的自定义模板忽略 DisplayMemberPath 的使用

Angular - 绑定(bind)公共(public)/私有(private)属性

c# - WPF 控件的扩展方法导致 StackOverflowException

wpf - 如何获取 System.Windows.Controls.Primitive.PopupRoot 类型?

c# - WPF 复合控件

c# - 绑定(bind)到 ObservableCollection 的 TextBox 不更新

c# - 如何将 TextBox 中的值绑定(bind)到 Mvvm Light 中的 View 模型

c# - Automapper - 无法映射嵌套对象/集合