C# 按钮命令在DataTemplate中不起作用

C# 按钮命令在DataTemplate中不起作用,c#,wpf,C#,Wpf,我创建了一个简单的测试解决方案来测试数据模板中的按钮,并将其设置为ContentControl。但是,在当前的实施中不起作用和响应 MainWindow.xam <Window x:Class="ButtonCommandWithDataTemplate.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft

我创建了一个简单的测试解决方案来测试数据模板中的按钮,并将其设置为ContentControl。但是,在当前的实施中不起作用和响应

MainWindow.xam

<Window x:Class="ButtonCommandWithDataTemplate.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ux="clr-namespace:ButtonCommandWithDataTemplate"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <ResourceDictionary>
        <DataTemplate x:Key="SimpleView" DataType="{x:Type ux:InnerViewModel}">
            <Button Command="{Binding ClickCommand}" Content="Click" />
        </DataTemplate>
    </ResourceDictionary>
</Window.Resources>
<Grid>
    <ContentControl >
        <ContentControl.Style>
            <Style TargetType="{x:Type ContentControl}">
                <Setter Property="ContentTemplate" Value="{StaticResource SimpleView}" />
            </Style>
        </ContentControl.Style>
    </ContentControl>
</Grid>
InnerViewModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace ButtonCommandWithDataTemplate
{
    public class MainWindowViewModel
    {
        private InnerViewModel innerVm;

        public InnerViewModel InnerVm
        {
            get { return innerVm; }
            set { innerVm = value; }
        }

        public MainWindowViewModel()
        {
            this.innerVm = new InnerViewModel();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace ButtonCommandWithDataTemplate
{
    public class InnerViewModel
    {
        private ICommand clickCommand;

        public ICommand ClickCommand
        {
            get
            {
                if (this.clickCommand == null)
                {
                    this.clickCommand = new RelayCommand(param =>
                    {
                        try
                        {
                            MessageBox.Show("W0w");
                        }
                        catch (Exception exception)
                        {
                            throw exception;
                        }
                    });
                }

                return this.clickCommand;
            }
        }
    }
}

在我下载的项目中,您有这个

<DataTemplate x:Key="SimpleView">
            <Button Command="{Binding Path=DataContext.ClickCommand, 
            RelativeSource={RelativeSource AncestorType={x:Type Window}}}" 
            Content="Click" />
</DataTemplate>

我把它改成了这个,它对我有用

<DataTemplate x:Key="SimpleView">
            <Button Command="{Binding Path=DataContext.InnerVm.ClickCommand, 
            RelativeSource={RelativeSource AncestorType={x:Type Window}}}" 
            Content="Click" />
</DataTemplate>


DataContext是没有Click命令的MainViewModel,它位于InnerViewModel上。

感谢您的指导!谢谢,它解决了我的问题。