Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从ContentControl中的按钮触发命令?_C#_Wpf_Mvvm_Mvvm Light_Contentcontrol - Fatal编程技术网

C# 从ContentControl中的按钮触发命令?

C# 从ContentControl中的按钮触发命令?,c#,wpf,mvvm,mvvm-light,contentcontrol,C#,Wpf,Mvvm,Mvvm Light,Contentcontrol,我是WPF新手,我正在尝试在ContentControl中动态添加一个按钮,当单击该按钮时应该会触发一个命令。我正在使用MVVMLight来处理命令 下面是一个带有两个按钮的示例。顶部按钮直接放置在StackPanel中。此按钮按预期触发命令 第二个按钮位于ContentControl中。它显示正确,但单击按钮时命令不会激发。 我假设这是因为绑定不会通过DataTemplate向下传输,但是如果我使用常规命令而不是MVVMLight RelayCommand,它似乎会工作 我不想删除这个框架,所

我是WPF新手,我正在尝试在ContentControl中动态添加一个按钮,当单击该按钮时应该会触发一个命令。我正在使用MVVMLight来处理命令

下面是一个带有两个按钮的示例。顶部按钮直接放置在StackPanel中。此按钮按预期触发命令

第二个按钮位于ContentControl中。它显示正确,但单击按钮时命令不会激发。 我假设这是因为绑定不会通过DataTemplate向下传输,但是如果我使用常规命令而不是MVVMLight RelayCommand,它似乎会工作

我不想删除这个框架,所以我想知道是否有人知道如何修复它?谢谢

<Window x:Class="ContentControlExample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:ContentControlExample.ViewModel">

    <Window.DataContext>
        <vm:MainViewModel />
    </Window.DataContext>

    <Window.Resources>
        <DataTemplate x:Key="MyButton" >
            <Button Content="SUBMIT" Command="{Binding MyCommand}" Width="200" Height="50"/>
        </DataTemplate>
    </Window.Resources>

    <StackPanel>

        <!--When this button is clicked, the Command executes as expected-->
        <Button Content="SUBMIT" Command="{Binding MyCommand}" Width="200" Height="50"/>


        <!--Nothing happens when this button is clicked-->
        <ContentControl ContentTemplate="{StaticResource MyButton}"/>
    </StackPanel>
</Window>

这里的问题是
ContentTemplate
中的隐式DataContext是
Content
,并且没有设置为任何值。您需要将
Content
设置为某个绑定,以桥接可视化树中当前的
DataContext
,如下所示:

<ContentControl ContentTemplate="{StaticResource MyButton}" Content="{Binding}"/>

另一种解决方案是为窗口命名:

<Window x:Class="ContentControlExample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:vm="clr-namespace:ContentControlExample.ViewModel"
    x:Name="_this">

然后通过其上下文进行绑定:

<Button Content="SUBMIT" Command="{Binding ElementName=_this, Path=DataContext.MyCommand}" Width="200" Height="50"/>


这对于ListView和ItemControls这样的东西特别方便,因为它们的DC被设置为列表元素。请记住,这只适用于同一可视树中的成员,如果不是这样(如弹出菜单等),则需要代理绑定。

代理绑定可能会有所帮助。看,谢谢!你的解决方案非常有效,这篇文章很好地解释了正在发生的事情以及如何修复它。我希望它是那么简单,但不幸的是,它没有改变anything@Karmacon我说的是对的,如果对你不起作用,也许你的问题中没有显示出另一个错误。我在问题中包含了我所有的代码,并尝试了你的建议,但什么都没有发生。@Karmacon只是自己尝试了代码,它工作得很好。你想让我发送一个演示项目吗?你能打开VS 2012项目吗(至少需要VS 2012)?
<Button Content="SUBMIT" Command="{Binding ElementName=_this, Path=DataContext.MyCommand}" Width="200" Height="50"/>