如何使用 DataTemplates 在运行时将通用窗口绑定到任意视图模型?
我有大量的 ViewModel 类。对于每个类,都有一个相应的 .xaml 文件,即“UserControl”。在我的 App.xaml 中,我将它们注册为 DataTemplates,如下所示:
<DataTemplate DataType="{x:Type viewModel:MainMenuViewModel}">
<view:MainMenuView/>
</DataTemplate>
其想法是 WPF 将能够在运行时自动交换必要的用户控件。例如,这是有效的:
<Grid>
<StackPanel>
<TextBlock Text="SuperApp" />
<ItemsControl>
<ViewModels:MainMenuViewModel/>
</ItemsControl>
</StackPanel>
</Grid>
条目“MainMenuViewModel”会自动替换为绑定到 MainMenuViewModel 的 MainMenuView。伟大的。我当前的目标是:我想要一个按钮,例如嵌入 MainMenuView 中的视图,它会打开一个弹出窗口,其中将有一个新的 ViewModel。我的想法是对其进行设置,以便我拥有一个“通用”弹出表单,在其中嵌入任意 ViewModel,并让 WPF 处理使用 DataTemplates 实际渲染它,与上面类似。所以我有一个绑定到按钮的命令,如下所示:
<Button Command="{Binding Path=LaunchInStandaloneForm}" Content="Rip Out"/>
它成功创建了一个新窗口,将 dataContext 设置为适当的 ViewModel,并显示该窗口。
问题是:如何设置此弹出窗口的 XAML,以便它为 ViewModel(即 DataContext)呈现适当的 DataTemplate?我已经尝试过:
<Grid>
<ItemsControl ItemsSource="{Binding Path=.}">
</ItemsControl>
</Grid>
,但它显示为空白。有什么指点吗?
I have a large number of ViewModel classes. For each of these classes, there is a corresponding .xaml file which is a 'UserControl'. In my App.xaml, I have them registered as DataTemplates, like so:
<DataTemplate DataType="{x:Type viewModel:MainMenuViewModel}">
<view:MainMenuView/>
</DataTemplate>
With the idea being that WPF will be able automatically swap in the necessary user controls at runtime. For example, this works:
<Grid>
<StackPanel>
<TextBlock Text="SuperApp" />
<ItemsControl>
<ViewModels:MainMenuViewModel/>
</ItemsControl>
</StackPanel>
</Grid>
In that the entry "MainMenuViewModel" is automatically replaced by the MainMenuView, bound to the MainMenuViewModel. Great. My current goal is now this: I want to have a button, on, say, a view embedded in the MainMenuView, which opens a popup window, which will have a new ViewModel inside. The idea is to set it up so that I have a single 'generic' popup form, in which I embed an arbitrary ViewModel, and let WPF handle actually rendering it with DataTemplates, similar to the above. So I have a command bound to a button, like so:
<Button Command="{Binding Path=LaunchInStandaloneForm}" Content="Rip Out"/>
Which successfully creates a new window, sets the dataContext equal to the appropriate ViewModel, and shows the window.
The question is: How do I set up the XAML of this popup window so that it will render the appropriate DataTemplate for the ViewModel which is the DataContext? I've tried:
<Grid>
<ItemsControl ItemsSource="{Binding Path=.}">
</ItemsControl>
</Grid>
, but it comes up blank. Any pointers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要将
ItemsSource
设置为DataContext
,请使用ItemsSource={Binding}
。假设DataContext
是视图模型对象的可枚举集合。更新正确答案:
使用
ContentControl
:)希望有所帮助。
To set the
ItemsSource
to theDataContext
, useItemsSource={Binding}
. That assumes that theDataContext
is an enumerable collection of your View Model objects.Updating with correct answer:
Use a
ContentControl
:)Hope that helps.
这里接受的答案显示了如何在运行时更改模板。从中你应该能够找出答案。有什么问题就喊吧。
如何修改silverlight组合框数据显示
希望有帮助
The accepted answer here shows how to change templates at runtime. You should be able to dig out the answer from that. Any questions just shout.
How to modify silverlight combobox data display
Hope that helps