C# WPF将按钮从窗口模板绑定到ViewModel';s命令

C# WPF将按钮从窗口模板绑定到ViewModel';s命令,c#,wpf,mvvm,devexpress,C#,Wpf,Mvvm,Devexpress,我有一扇像这样的窗户 正如您所看到的,“窗口栏”上有一个按钮,我想将按钮命令绑定到ViewModel的命令 下面是可视化树的外观 我尝试过使用RelativeSource进行各种组合,但找不到一种方法来实现这一点 任何形式的帮助或想法都将不胜感激 窗口本身的代码 <dx:DXWindow x:Class="Chronos.WindowsApp.Windows.TimersCollectionWindow" xmlns="http://schemas.micro

我有一扇像这样的窗户

正如您所看到的,“窗口栏”上有一个按钮,我想将按钮命令绑定到ViewModel的命令

下面是可视化树的外观

我尝试过使用RelativeSource进行各种组合,但找不到一种方法来实现这一点

任何形式的帮助或想法都将不胜感激

窗口本身的代码

<dx:DXWindow x:Class="Chronos.WindowsApp.Windows.TimersCollectionWindow"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:themes="http://schemas.devexpress.com/winfx/2008/xaml/core/themekeys"
             mc:Ignorable="d" 
             Title="Timers" 
             ShowInTaskbar = "False" 
             ShowIcon="True" Icon="/Chronos.UserControls;component/Images/TimersWindowIconW.png"
             d:DesignHeight="80" d:DesignWidth="80"
             >

    <dx:DXWindow.Resources>
        <ControlTemplate x:Key="{themes:FloatingContainerThemeKey ThemeName=Mishcon, ResourceKey=FloatingContainerDragWidgetTemplate, IsThemeIndependent=True}" TargetType="{x:Type Thumb}">
            <Border Height="40" Background="Transparent" DockPanel.Dock="Left">
                <DockPanel HorizontalAlignment="Left">
                    <Button DockPanel.Dock="Left" Background="Transparent"
                            Width="70"
                            Height="35"
                            Command="{Binding RelativeSource={RelativeSource AncestorType=dx:DXWindow}, Path=RootControl.DataContext.NewTimmerCommand}"
                            >
                        <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                            <Image Source="{dx:DXImage Image=Add_32x32.png}" Width="24" VerticalAlignment="Center"/>
                            <TextBlock Text="Add" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </Button>
                </DockPanel>
            </Border>
        </ControlTemplate>
    </dx:DXWindow.Resources>
</dx:DXWindow>

我得到的错误是:

System.Windows.Data错误:40:BindingExpression路径错误: 在“对象”“TimerCollectionWindow”上找不到“RootControl”属性 (名称=“”)。 BindingExpression:Path=RootControl.DataContext.NewTimmerCommand; DataItem='TimerCollectionWindow'(名称='');目标元素是 '按钮'(名称='');目标属性为“Command”(类型为“ICommand”)


正如我从绑定表达式错误中看到的,它试图查找路径的对象是TimerCollectionWindow,如果您的命令存在于该类中 那么我想你应该这样做一个直接绑定:

按钮xaml代码

<Button DockPanel.Dock="Left" Background="Transparent"
                        Width="70"
                        Height="35"
                        Command="{Binding NewTimmerCommand, UpdateSourceTrigger=PropertyChanged, Mode = TwoWay}">
 <Button DockPanel.Dock="Left" Background="Transparent"
                        Width="70"
                        Height="35"
                        Command="{Binding 
                    RelativeSource={RelativeSource AncestorType={x:Type Thumb}}, 
                    Path=DataContext.NewTimmerCommand}">
<Button DockPanel.Dock="Left" Background="Transparent"
                        Width="70"
                        Height="35"
                        Command="{Binding 
                    RelativeSource={RelativeSource AncestorType={x:Type the_type_of_RootControl}}, 
                    Path=DataContext.NewTimmerCommand}">
如果您的命令进入使用已定义ContentTemplate的对象的数据上下文,则只需执行下一步。由于ContentTemplate的目标类型是Thumb,它肯定会在ContentTemplate使用时创建,Thumb将从其父级继承其DataContext,因此您可以依赖与Thumb的绑定,并从其(及其父级)数据上下文获取命令

XAML按钮代码

<Button DockPanel.Dock="Left" Background="Transparent"
                        Width="70"
                        Height="35"
                        Command="{Binding NewTimmerCommand, UpdateSourceTrigger=PropertyChanged, Mode = TwoWay}">
 <Button DockPanel.Dock="Left" Background="Transparent"
                        Width="70"
                        Height="35"
                        Command="{Binding 
                    RelativeSource={RelativeSource AncestorType={x:Type Thumb}}, 
                    Path=DataContext.NewTimmerCommand}">
<Button DockPanel.Dock="Left" Background="Transparent"
                        Width="70"
                        Height="35"
                        Command="{Binding 
                    RelativeSource={RelativeSource AncestorType={x:Type the_type_of_RootControl}}, 
                    Path=DataContext.NewTimmerCommand}">

更新

<Button DockPanel.Dock="Left" Background="Transparent"
                        Width="70"
                        Height="35"
                        Command="{Binding NewTimmerCommand, UpdateSourceTrigger=PropertyChanged, Mode = TwoWay}">
 <Button DockPanel.Dock="Left" Background="Transparent"
                        Width="70"
                        Height="35"
                        Command="{Binding 
                    RelativeSource={RelativeSource AncestorType={x:Type Thumb}}, 
                    Path=DataContext.NewTimmerCommand}">
<Button DockPanel.Dock="Left" Background="Transparent"
                        Width="70"
                        Height="35"
                        Command="{Binding 
                    RelativeSource={RelativeSource AncestorType={x:Type the_type_of_RootControl}}, 
                    Path=DataContext.NewTimmerCommand}">


问候。

您是否尝试过
{Binding RelativeSource={RelativeSource AncestorType=dx:DXWindow},Path=DataContext.newtinmercommand}“
?能否显示该窗口的XAML(简化)简而言之-documentManager服务创建窗口,然后创建视图,然后创建视图模型,将其分配给视图,然后将视图放置在window@StepUp-这可能不够。据我所知,没有填充窗口的DataContext属性。@Marty-能否尝试为窗口设置DataContext并使用so@stepp给出的解析?是的,这就是问题所在..该命令不存在于'TimerWindow'类中..该命令存在于'TimerCollectionViewModel'中,该'TimerCollectionViewModel'作为dataContext分配给tree@MartyRootControl是在哪里定义的?@Marty只是使用此数据模板的RootControl?RootControl“-是我的实际视图(从MVVM)它附加了一个viewModel。到达后,“documentManagerService”启动,并负责将视图放置到“指定”窗口、面板、框架或选项卡中。指定thumb也没有帮助。错误:System.Windows.Data错误:40:BindingExpression路径错误:“object”“BackgroundPan上找不到NewTimmerCommand”属性el'(名称='').BindingExpression:Path=DataContext.NewTimmerCommand;DataItem='Thumb'(名称='PART'DragWidget');目标元素为'Button'(名称='');目标属性为'Command'(类型为'ICommand')