在 WPF 中使用 DataTemplate 和模拟对象
我有以下 xaml 代码:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding MainWindow, Source={StaticResource Locator}}">
<Window.Resources>
<DataTemplate DataType="{x:Type vm:KeyboardViewModel}">
<vw:Keyboard />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:WelcomeViewModel}">
<vw:Welcome />
</DataTemplate>
</Window.Resources>
<DockPanel>
<DockPanel>
<ContentControl Content="{Binding Path=Workspace}" />
</DockPanel>
</DockPanel>
</Window>
当工作区是 KeyboardViewModel 时,将显示 UserControl 键盘。当工作区处于欢迎状态时,将显示欢迎屏幕。但是当我测试时,我使用 Moq 模拟 ViewModel。然后,工作区获取类型 IKeyboardViewModelProxyxxxxxxxxxxxxx(其中 xxxxxxx 是随机字符串),该类型不会映射到 DataTemplate 中的 KeyboardViewModel,并且 WPF 现在不希望 DataTemplate 显示。 当我使用真正的KeyboardViewModel时,没有问题。 我可以以某种方式修复它,还是必须重新设计它?
I have following xaml code:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding MainWindow, Source={StaticResource Locator}}">
<Window.Resources>
<DataTemplate DataType="{x:Type vm:KeyboardViewModel}">
<vw:Keyboard />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:WelcomeViewModel}">
<vw:Welcome />
</DataTemplate>
</Window.Resources>
<DockPanel>
<DockPanel>
<ContentControl Content="{Binding Path=Workspace}" />
</DockPanel>
</DockPanel>
</Window>
When Workspace is KeyboardViewModel, then the UserControl Keyboard is shown. When Workspace is Welcome, then the Welcome screen is shown. But when I test I mock the ViewModels with Moq. Workspace then get the type IKeyboardViewModelProxyxxxxxxxxxxxxx (where xxxxxxx is a random string), that don't maps to KeyboardViewModel in the DataTemplate and WPF don't now wish DataTemplate to show.
When I use the real KeyboardViewModel, it is no problem.
Can I fix it somehow, or do I have to redesign it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了类似的问题(但是没有使用起订量)。我使用的部分解决方案是从抽象 KeyboardViewModelAbstract 继承 KeyboardViewModel 和 KeyboardViewModelMock。然后你可以这样做:
这对真实模型对象和模拟对象都适用。
不幸的是,当您处理已经具有基类或涉及任何类型的继承的模型时,此解决方案无法扩展。如果 DataTemplate 可以与接口一起使用,那就太好了,但它们不能。
I'm having a similar issue (without using Moq however). A PARTIAL solution that I used is to inherit both KeyboardViewModel and KeyboardViewModelMock from abstract KeyboardViewModelAbstract. Then you can do:
Which will work for both, the real model object and the mock.
Unfortunately this solution doesn't scale when you're dealing with models that already have a base class or have any kind of inheritance involved. I'd be great if DataTemplate could be used with interfaces, but they can't.
您可以省略
DataType="{x:Type vm:KeyboardViewModel}"
。如果您这样做,则不再期望绑定 KeyboardViewModel 类型的实例,而是仅绑定仅具有模板中使用的所有属性的任何类型的对象。You can omit the
DataType="{x:Type vm:KeyboardViewModel}"
. If you do that, it is not expecting an instance of type KeyboardViewModel to bind against anymore but only an object of any type that just has all properties that are used in the template.