Contentcontrol 找不到我的数据模板,它是 ViewModel
我正在使用 WPF、MVVM 和棱镜。 我的视图中有一个链接到 ViewModel UC2002_RFPBeheren_ViewModel 的数据模板,因为包含此代码的页面链接到另一个 ViewModel,并且我希望按钮将 UC2002_RFPBeheren_ViewModel 作为 ViewModel。
此页面的数据上下文是 UC2002_RFPBeheren_ProjectInfo_ViewModel,但我希望 SaveButton 使用 ViewModel UC2002_RFPBeheren_ViewModel
这是我的代码:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Resources/RFPModuleResources.xaml" />
<ResourceDictionary>
<DataTemplate x:Key="SaveButton" DataType="{x:Type vm:UC2002_RFPBeheren_ViewModel}">
<Button Command="{Binding SaveRFPCommand}">Save</Button>
</DataTemplate>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<StackPanel HorizontalAlignment="Right" Orientation="Horizontal">
<ContentControl ContentTemplate="{StaticResource SaveButton}"/>
<Button Command="{Binding CloseTabCommand}">Close</Button>
</StackPanel>
虽然 SaveButton 显示但不对我的命令做出反应。
我是否忘记了什么或者有其他方法可以解决这个问题吗?
提前致谢 ;) !
=================================================== ===============================
编辑:
所以我做了一些更改,但它仍然没有'不工作。
代码示例:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Resources/RFPModuleResources.xaml" />
<ResourceDictionary>
<DataTemplate x:Key="SaveButton" DataType="{x:Type vm:UC2002_RFPBeheren_ViewModel}">
<Button Command="{Binding SaveRFPCommand}">Save</Button>
</DataTemplate>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
我在页面的 ViewModel 中设置此属性
public UC2002_RFPBeheren_ViewModel MySaveVM { get; set; }
我的 stackpanel 现在看起来像这样:
<StackPanel HorizontalAlignment="Right" Orientation="Horizontal">
<ContentControl Content="{Binding MySaveVM}" ContentTemplate="{StaticResource SaveButton}"/>
<Button Command="{Binding CloseTabCommand}">Close</Button>
</StackPanel>
I'm using WPF, MVVM & PRISM.
I got a datatemplate in my View linked to a ViewModel UC2002_RFPBeheren_ViewModel cause the page were this code is included is linked to another ViewModel and I want the Button to have UC2002_RFPBeheren_ViewModel as ViewModel.
The datacontext of this page is UC2002_RFPBeheren_ProjectInfo_ViewModel but I want the SaveButton to use the ViewModel UC2002_RFPBeheren_ViewModel
Here is my code:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Resources/RFPModuleResources.xaml" />
<ResourceDictionary>
<DataTemplate x:Key="SaveButton" DataType="{x:Type vm:UC2002_RFPBeheren_ViewModel}">
<Button Command="{Binding SaveRFPCommand}">Save</Button>
</DataTemplate>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<StackPanel HorizontalAlignment="Right" Orientation="Horizontal">
<ContentControl ContentTemplate="{StaticResource SaveButton}"/>
<Button Command="{Binding CloseTabCommand}">Close</Button>
</StackPanel>
Although the SaveButton displays but don't reacts on my command.
Do I forget something or is there another way to solve this?
Thanks in advance ;) !
=================================================================================
EDIT:
So I made some changes but it still doesn't work.
Code example:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Resources/RFPModuleResources.xaml" />
<ResourceDictionary>
<DataTemplate x:Key="SaveButton" DataType="{x:Type vm:UC2002_RFPBeheren_ViewModel}">
<Button Command="{Binding SaveRFPCommand}">Save</Button>
</DataTemplate>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
I set this property in the ViewModel of the page
public UC2002_RFPBeheren_ViewModel MySaveVM { get; set; }
My stackpanel looks now like this:
<StackPanel HorizontalAlignment="Right" Orientation="Horizontal">
<ContentControl Content="{Binding MySaveVM}" ContentTemplate="{StaticResource SaveButton}"/>
<Button Command="{Binding CloseTabCommand}">Close</Button>
</StackPanel>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用 MVVM,则必须在 UC2002_RFPBeheren_ProjectInfo_ViewModel 中公开一些 UC2002_RFPBeheren_ViewModel 实例以进行绑定。作为属性,或者作为属性的集合的项目。
视图中的所有内容最终都必须可以从作为数据上下文的 ViewModel 进行访问 (UC2002_RFPBeheren_ProjectInfo_ViewModel)
If you are using MVVM you must expose some instance of UC2002_RFPBeheren_ViewModel within your UC2002_RFPBeheren_ProjectInfo_ViewModel to bind against. Either as a property, or an item of a collection that is a property.
Everything in the view must be ultimately accessible from the ViewModel that is your data context (UC2002_RFPBeheren_ProjectInfo_ViewModel)
如果将 UV2002_RFPBeheren_ViewModel 实例设置为 ContentPresenter 的内容,会发生什么?
DataTemplate 只是说明视图模型应如何显示,但您必须将 DataContext 或 Binding 设置为视图模型的实例。
编辑:
示例
xaml
编辑:小型工作示例
Viewmodels
what happens if you set your UV2002_RFPBeheren_ViewModel instance as the content for the ContentPresenter?
the DataTemplate just say how your Viewmodel should be displayed, but you have to set the DataContext or Binding to the instance of your viewmodel.
EDIT:
example
xaml
EDIT: Small working sample
Viewmodels
这是由 ContentControl 的工作方式决定的。假定 ContentTemplate 中的内容与内容相关,因此 DataContext 设置为内容,因此模板中的按钮可以访问 DataContext。您尚未指定 Content,因此该值为 null,因此 DataContext 显式设置为 null。您可以在一个基本示例中看到这一点。您可以做的一件事是将 ContentControl 的内容绑定到 DataContext - 请参阅示例中的最后一个 contentcontrol。
This is because of the way that ContentControl's work. It is assumed that the things in the ContentTemplate are related to the Content and so the DataContext is set to the Content and therefore that is the DataContext that the button within the template has access to. You haven't specified a Content so the value is null and so the DataContext is explicitly set to null. You can see this in a basic example. One thing you can do is to bind the Content of the ContentControl to the DataContext - see the last contentcontrol in the example.