如何将 WPF 控件的内容绑定到其容器的 DataContext,以便我可以基于该对象应用 DataTemplateSelector?
我正在尝试将 WPF 窗口绑定到包含两个集合 A 和 B 的 ViewModel 之上。我正在尝试使用 DataTemplates 根据 ViewModel 中标志的设置来显示 A 或 B。
为此,我设置了窗口的 DataContext = ViewModel
。但是,当我尝试将 ContentControl
绑定到该 DataContext 并向其应用 DataTemplateSelector
时,选择器的 SelectTemplate 的
方法始终为 null:item
参数(对象项,DependencyObject 容器)
<Window [snip] Title="MainWindow">
<Window.Resources>
<!-- DataTemplate and Selector declarations -->
</Window.Resources>
<Grid>
<ContentControl Content="{Binding}"
ContentTemplateSelector="{StaticResource templateSelector}" />
</Grid>
</Window>
我应该如何绑定该 ContentControl
以便将 Window 的 ViewModel 传递到其 DataTemplateSelector
?
I'm trying to bind a WPF window atop a ViewModel that contains two collections, A and B. I'm attempting to use DataTemplates to display either A or B depending on the setting of a flag in my ViewModel.
To that end, I've set the window's DataContext = ViewModel
. However, when I attempt to bind a ContentControl
to that DataContext and apply a DataTemplateSelector
to it, the item
parameter of the selector's SelectTemplate(object item, DependencyObject container)
method is always null:
<Window [snip] Title="MainWindow">
<Window.Resources>
<!-- DataTemplate and Selector declarations -->
</Window.Resources>
<Grid>
<ContentControl Content="{Binding}"
ContentTemplateSelector="{StaticResource templateSelector}" />
</Grid>
</Window>
How should I be binding that ContentControl
such that the Window's ViewModel will be passed through to its DataTemplateSelector
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这对我有用:
编辑:
不过我同意亚伦的观点,这可能不是完成任务的最佳方式。你说你正在使用 ViewModel。最简单的方法可能是将 ItemsControl 绑定到 Viewmodel 上公开所需集合的新“SelectedCollection”属性。然后在您的标志中(假设它是一个属性)您可以为“SelectedCollection”触发 propertychanged。
this worked for me:
Edit:
I agree with Aaron though, that this might not be the best way to accomplish things. You said you're using a ViewModel. The easiest way would probably be to bind your ItemsControl to a new "SelectedCollection" property on your Viewmodel that exposes the wanted collection. Then in your flag (assuming it is a property) you can fire propertychanged for "SelectedCollection".
这里发生了很多事情...
您说您正在使用
DataTemplateSelector
来显示集合 A 或集合 B,同时您表示您正在将其中一个集合设置为DataContext。
如果您想隐藏一个集合中的数据,请对集合本身执行过滤。另一种方法是通过
IValueConverter
或IMultiValueConverter
运行绑定。另一种解决方案可能是将两个 UI 元素分别绑定到每个集合,并根据 ViewModel 中的值更改 UI 元素的可见性。有很多不同的选项...如果我理解正确的话,
DataTemplateSelector
不应该是其中之一。Lots of things going on here...
You said you are using the
DataTemplateSelector
to either display collection A or collection B, while at the same time you stated you are setting one of the collections as theDataContext
of the Window.If you want to hide the data in one collection perform filtering on the collection itself. Another approach is to run the binding through an
IValueConverter
orIMultiValueConverter
. Yet another solution could be to have two UI elements bound to each collection respectively and change theVisiblity
of the UI element based on the value in your ViewModel.Lot's of different options...and if I understand you correctly the
DataTemplateSelector
should not be one of them.