为什么WPF绑定需要getter和setter ?

时间:2021-05-06 20:12:23

If I have a WPF listbox and I bind its itemssource to a list of objects. If the object members are public but don't have a { get; set; } the binding will fail. Why?

如果我有一个WPF列表框,并将其itemssource绑定到对象列表。如果对象成员是公共的,但没有{get;设置;绑定将失败。为什么?

2 个解决方案

#1


6  

I think what you're really asking is "Why do I have to use properties instead of just fields?" And the answer is that that's just how WPF bindings work. You have to bind to properties on objects. The binding system doesn't look for matching fields.

我认为你真正想问的是“为什么我必须使用属性而不是字段?”答案是这就是WPF绑定的工作方式。你必须绑定到对象上的属性。绑定系统不会寻找匹配的字段。

#2


4  

The binding is looking specifically for Properties, not fields. That's why bindings work on PropertyPath objects.

绑定是专门针对属性,而不是字段。这就是绑定在PropertyPath对象上工作的原因。

#1


6  

I think what you're really asking is "Why do I have to use properties instead of just fields?" And the answer is that that's just how WPF bindings work. You have to bind to properties on objects. The binding system doesn't look for matching fields.

我认为你真正想问的是“为什么我必须使用属性而不是字段?”答案是这就是WPF绑定的工作方式。你必须绑定到对象上的属性。绑定系统不会寻找匹配的字段。

#2


4  

The binding is looking specifically for Properties, not fields. That's why bindings work on PropertyPath objects.

绑定是专门针对属性,而不是字段。这就是绑定在PropertyPath对象上工作的原因。