簡體   English   中英

WPF將一個按鈕從Window模板綁定到ViewModel的命令

[英]WPF bind a button from Window template to ViewModel's command

我有一個看起來像這樣的窗口

在此輸入圖像描述

正如你可以看到' 窗口欄 '本身有一個按鈕 ,我想將按鈕命令綁定到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路徑錯誤:'對象'上找不到'RootControl'屬性'''TimersCollectionWindow'(Name ='')'。 BindingExpression:路徑= RootControl.DataContext.NewTimmerCommand; DataItem ='TimersCollectionWindow'(Name =''); target元素是'Button'(Name =''); target屬性是'Command'(類型'ICommand')

正如我從你的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}">

問候。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM