简单的WPF绑定不起作用

编程入门 行业动态 更新时间:2024-10-26 19:27:47
本文介绍了简单的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绑定不起作用

本文发布于:2023-11-10 06:05:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574551.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   不起作用   简单   WPF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!