StackPanel 有一个 DataContext,如何使用另一个选项值填充 ListPicker?
我是 WP7 开发的新手,所以绑定对我来说还是有点陌生。我有一个 StackPanel,我已使用 TwoWay 绑定设置 DataContext 来编辑给定的记录。但是,在“表单”中,我有一个 ListPicker,我想用查找表中的可能值填充它。
目前,我已经创建了一个 DataTemplate 并设置了 ListPicker 的 ItemsSource 属性。由于 DataTemplate 中唯一的 TextBlock 绑定到 ItemsSource 数据上下文,因此如何将 ListPicker 的 SelectedItem 绑定到 StackPanel 的数据上下文?
I'm new to WP7 development, so binding is still a little foreign to me. I have a StackPanel that I've set the DataContext with TwoWay binding for editing a given record. But, within the "form" I have a ListPicker that I want to populate with possible values from a lookup table.
Currently I've created a DataTemplate and set the ItemsSource property of the ListPicker. Since the sole TextBlock in the DataTemplate is binding to the ItemsSource data context, how do I then bind the ListPicker's SelectedItem to the StackPanel's data context?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
分配给 StackPanel 的 DataContext 的对象必须公开适当的属性,例如:
然后您需要绑定 ListPicker 的 ItemsSource:
ItemsSource={Binding Data}
和 ListPicker 的 SelectedItem:SelectedItem={Binding Selected}
。如果您希望 ListPicker 在更改 MyData 的 Selected 属性时做出反应,则需要
MyData
类来实现接口IObservable
。Data
属性也是如此,该集合必须在其状态更改时发出通知,因此使其类型为:ObservableCollection
。Your object that you assign to StackPanel's DataContext must expose appropriate properties, for example:
Then you need to bind ListPicker's ItemsSource:
ItemsSource={Binding Data}
and ListPicker's SelectedItem:SelectedItem={Binding Selected}
.If you want ListPicker to react when you change MyData's Selected property you will need
MyData
class to implement interfaceIObservable
. The same goes forData
property, this collection must inform when it's state changes, so make it of type:ObservableCollection<ElemType>
.检查以下内容:使用父级的 DataContext(WPF - 动态菜单命令绑定)
检查所有答案看看其中之一是否对您有帮助。
Check this: Using the parent's DataContext (WPF - Dynamic Menu Command Binding)
Check all answers and see if one of them helps you.