简单的WPF绑定不起作用 [英] Simple WPF Binding not working

查看:78
本文介绍了简单的WPF绑定不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习WPF,但是我一直坚持使用数据绑定.我有一个TreeView,将ItemSource设置为ObserveableCollection<UIBrowserItem>.

I'm learning WPF, and I'm stuck with Data Bindings. I have a TreeView, which ItemSource is set to a ObserveableCollection<UIBrowserItem>.

我的绑定看起来像:

<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中的项目不会设置标题.

But the Items in the TreeView won't have a header set ..

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

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; }
}

如果标题可以在运行时更改,则还需要实现INotifyPropertyChanged.

If the title can change at run time, you also need to implement INotifyPropertyChanged.

这篇关于简单的WPF绑定不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆