c# - Contentcontrol 找不到我的数据模板,它是 ViewModel

标签 c# wpf xaml mvvm prism

我正在使用 WPF、MVVM 和 PRISM。 我的 View 中有一个链接到 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>

虽然保存按钮显示,但对我的命令没有反应。
我是否忘记了什么或者有其他方法可以解决这个问题吗?
提前致谢;)!

================================================== ===================================

编辑:

所以我做了一些更改,但仍然不起作用。

代码示例:

<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 HorizontalAlignment="Right" Orientation="Horizontal">
    <ContentControl Content="{Binding MySaveVM}" ContentTemplate="{StaticResource SaveButton}"/>
    <Button Command="{Binding CloseTabCommand}">Close</Button>
</StackPanel>

最佳答案

如果将 UV2002_RFPBeheren_ViewModel 实例设置为 ContentPresenter 的内容,会发生什么?

 <ContentControl Content="{Binding MyUV2002_RFPBeheren_ViewModel}"/>

DataTemplate 只是说明 View 模型应如何显示,但您必须将 DataContext 或 Binding 设置为 View 模型的实例。

编辑:

示例

 public class VMFoo
 {
     public UV2002_RFPBeheren_ViewModel MySaveVM {get; set;}
 }

xaml

<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>
<UserControl.DataContext>
   <x:local VMFoo/>
</UserControl.DataContext>
<StackPanel HorizontalAlignment="Right" Orientation="Horizontal">
   <ContentControl Content="{Binding MySaveVM}"/>
   <Button Command="{Binding CloseTabCommand}">Close</Button>
</StackPanel>

编辑:小型工作样本

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:WpfApplication1="clr-namespace:WpfApplication1" Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <ResourceDictionary>
        <DataTemplate DataType="{x:Type WpfApplication1:UV2002_RFPBeheren_ViewModel}">
            <Button Command="{Binding SaveRFPCommand}">Save</Button>
        </DataTemplate>
    </ResourceDictionary>
</Window.Resources>
<Grid> 
    <Grid.DataContext>
        <WpfApplication1:VMFoo/>
    </Grid.DataContext>
    <StackPanel HorizontalAlignment="Right" Orientation="Horizontal">
        <ContentControl Content="{Binding MySaveVM}"/>
        <Button Command="{Binding CloseTabCommand}">Close</Button>
    </StackPanel>
</Grid>
</Window>

View 模型

public class VMFoo
{
    public VMFoo()
    {
        this.MySaveVM = new UV2002_RFPBeheren_ViewModel();
    }
    public UV2002_RFPBeheren_ViewModel MySaveVM { get; set; }
}

public class UV2002_RFPBeheren_ViewModel
{
    private DelegateCommand _save;
    public ICommand SaveRFPCommand
    {
        get{if(this._save==null)
        {
            this._save = new DelegateCommand(()=>MessageBox.Show("success"),()=>true);
        }
            return this._save;
        }
    }
}

关于c# - Contentcontrol 找不到我的数据模板,它是 ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7916382/

相关文章:

c# - 如何使用 mvvmLight 将数据从 ContentPage ViewModel 传递到 Xamarin Forms 中的 ContentView Viewmodel?

c# - List<T>.IndexOf() 与 List<T>.FindIndex() 的效率

c# - 将 100,000 多条记录插入 DocumentDB 的最快方法

c# - 当对象的属性更改时,WPF 在父级上调用 updatesourcetrigger xaml

c# - 使用 ViewModel 方法的命令绑定(bind)

c# - 使用自定义 ItemsSource 刷新 ListBox 的最简单方法?

c# - Crystal 报表登录失败问题

C#:从 XML 架构 (XSD) 文件自动生成 DDL 和 ORM 类

c# - 如何从 WPF 中 FlowDocument 的表中的 TableRow 获取 TableCell 值?

xaml - 如何更改 ContentDialog 过渡?