我是WPF编程的新手,我脑子里都有这个问题.
为什么绑定仅在使用属性而不是字段时有效?
这是给我这个问题的例子:
XAML:
<ListBox ItemsSource="{Binding requiredPermissions}" DisplayMemberPath="MyText" Grid.Row="1" ></ListBox>
Run Code Online (Sandbox Code Playgroud)
代码背后(作品):
public ObservableCollection<MyNotifyableText> requiredPermissions { get; set; }
Run Code Online (Sandbox Code Playgroud)
代码背后(不起作用):
public ObservableCollection<MyNotifyableText> requiredPermissions;
Run Code Online (Sandbox Code Playgroud)
提前致谢.
为什么在使用属性而不是字段时绑定有效?
因为微软使用反射幕后在运行时解决结合的路径,他们只搜索公共属性的的DataContext
使用Type.GetProperty方法.
所以你只能绑定到公共属性 - 而不是字段.