WPF将一个按钮从Window模板绑定到ViewModel的命令

时间:2016-02-04 14:25:03

标签: c# wpf mvvm devexpress

我有一个看起来像这样的窗口

enter image description here

正如你可以看到'窗口栏'本身有一个按钮,我想将按钮命令绑定到ViewModel的命令

以下是可视树的外观

enter image description here

我尝试使用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路径错误:   在'object'''TimersCollectionWindow'上找不到'RootControl'属性   (名称= '')”。   BindingExpression:路径= RootControl.DataContext.NewTimmerCommand;   DataItem ='TimersCollectionWindow'(Name ='');目标元素是   'Button'(Name =''); target属性是'Command'(类型'ICommand')

1 个答案:

答案 0 :(得分:0)

正如我从你的Binding Expression错误中看到的那样,如果你的命令存在于这个类中,那么它试图找到你的路径的对象是TimersCollectionWindow 那么我认为你应该像这样直接绑定:

按钮xaml代码

<Button DockPanel.Dock="Left" Background="Transparent"
                        Width="70"
                        Height="35"
                        Command="{Binding NewTimmerCommand, UpdateSourceTrigger=PropertyChanged, Mode = TwoWay}">

VM命令声明

    public ICommand NewTimmerCommand
    {
        get { return _newTimmerCommand; }
        set
        {
            _newTimmerCommand = value;
            OnPropertyChanged("NewTimmerCommand");
        }
    }

如果您的命令是使用已定义的ContentTemplate的对象的数据上下文,那么只需执行下一步。由于您的ContentTemplate的目标类型是Thumb,它将在ContentTemplate正在使用时准确创建,Thumb将从其父级继承其DataContext,因此您可以依赖绑定到Thumb并从中获取命令它(及其父级)数据上下文。

XAML按钮代码

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

问候。