WPF 将 Window 模板中的按钮绑定到 ViewModel 的命令
WPF bind a button from Window template to ViewModel's command
我有一个 window 看起来像这样
如您所见,'window bar' 本身有一个 button,我想绑定按钮命令到 ViewModel 的命令
这是可视化树的样子
我已经尝试了各种使用 RelativeSource 的组合,但无法找到一种方法来实现它..
如有任何帮助或想法,我们将不胜感激
window 本身的代码..
<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 Error: 40 : BindingExpression path error:
'RootControl' property not found on 'object' ''TimersCollectionWindow'
(Name='')'.
BindingExpression:Path=RootControl.DataContext.NewTimmerCommand;
DataItem='TimersCollectionWindow' (Name=''); target element is
'Button' (Name=''); target property is 'Command' (type 'ICommand')
正如我从你的绑定表达式错误中看到的那样, 上的对象试图找到你的路径是 TimersCollectionWindow,如果你的命令存在于此 class
那么我认为你应该像这样直接绑定:
按钮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}">
此致。
我有一个 window 看起来像这样
如您所见,'window bar' 本身有一个 button,我想绑定按钮命令到 ViewModel 的命令
这是可视化树的样子
我已经尝试了各种使用 RelativeSource 的组合,但无法找到一种方法来实现它..
如有任何帮助或想法,我们将不胜感激
window 本身的代码..
<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 Error: 40 : BindingExpression path error: 'RootControl' property not found on 'object' ''TimersCollectionWindow' (Name='')'. BindingExpression:Path=RootControl.DataContext.NewTimmerCommand; DataItem='TimersCollectionWindow' (Name=''); target element is 'Button' (Name=''); target property is 'Command' (type 'ICommand')
正如我从你的绑定表达式错误中看到的那样, 上的对象试图找到你的路径是 TimersCollectionWindow,如果你的命令存在于此 class 那么我认为你应该像这样直接绑定:
按钮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}">
此致。