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

发布于 2024-12-12 19:46:38 字数 2521 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

放我走吧 2024-12-19 19:46:40

如果您使用 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)

蓝眸 2024-12-19 19:46:39

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

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

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

编辑:

示例

 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>

Viewmodels

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;
        }
    }
}

what happens if you set your UV2002_RFPBeheren_ViewModel instance as the content for the ContentPresenter?

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

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

 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>

EDIT: Small working sample

<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>

Viewmodels

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;
        }
    }
}
一人独醉 2024-12-19 19:46:39

这是由 ContentControl 的工作方式决定的。假定 ContentTemplate 中的内容与内容相关,因此 DataContext 设置为内容,因此模板中的按钮可以访问 DataContext。您尚未指定 Content,因此该值为 null,因此 DataContext 显式设置为 null。您可以在一个基本示例中看到这一点。您可以做的一件事是将 ContentControl 的内容绑定到 DataContext - 请参阅示例中的最后一个 contentcontrol。

<StackPanel DataContext="Foo">  
  <StackPanel.Resources>
    <DataTemplate x:Key="withBtn">
      <Button Content="{Binding}" />
    </DataTemplate>
  </StackPanel.Resources>
  <Button Content="{Binding}" />
  <ContentControl ContentTemplate="{StaticResource withBtn}" />
  <ContentControl Content="{Binding}" ContentTemplate="{StaticResource withBtn}" />
</StackPanel>

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.

<StackPanel DataContext="Foo">  
  <StackPanel.Resources>
    <DataTemplate x:Key="withBtn">
      <Button Content="{Binding}" />
    </DataTemplate>
  </StackPanel.Resources>
  <Button Content="{Binding}" />
  <ContentControl ContentTemplate="{StaticResource withBtn}" />
  <ContentControl Content="{Binding}" ContentTemplate="{StaticResource withBtn}" />
</StackPanel>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文