什么是之间的差异的RoutedCommand和RelayCommand?何时使用RoutedCommand以及何时在MVVM模式中使用RelayCommand?
让我们有一个Command绑定到自定义命令的按钮属性.
我什么时候应该实施ICommand,何时衍生出来RoutedCommand?我看到RoutedCommand实现了ICommand.
在哪种情况下我需要补充一个ICommand?那么MVVM模型呢?哪一种更适合这个目的?
我很难理解RoutedCommand的CommandTarget属性.
基本上,我有一些静态命令,在用户控件(而不是窗口)中有实现.我在用户控件中创建了一个命令绑定.如果我在usercontrol中声明了按钮,那么我就可以使用我的路由事件了.但是,当按钮位于usercontrol之外时,我无法使用我的路由事件.我认为命令目标将解决我的问题.
那么如何为工具栏usercontrol的按钮设置commandtarget,以便调用Container的Executed和CanExecuted?
编辑代码与micahtan更改的更改,但我仍然无法将其发送到CanExecute或Execute.
窗口XAML:
<Window x:Class="RoutedCommands.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:RoutedCommands"
xmlns:toolbar="clr-namespace:RoutedCommands.Toolbar"
Title="Window1" Height="300" Width="300">
<StackPanel>
<local:Container Width="100" Height="25" x:Name="MyContainer" />
<toolbar:Toolbar Width="100" Height="25" CommandTarget="{Binding MyContainer}" />
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
工具栏XAML:
<UserControl x:Class="RoutedCommands.Toolbar.Toolbar"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:RoutedCommands"
x:Name="MyToolbar"
Height="300" Width="300">
<Grid>
<Button Command="{x:Static local:Commands.MyCommand}" Content="Try Me" CommandTarget="{Binding ElementName=MyToolbar, Path=CommandTarget, Mode=OneWay}" />
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
工具栏CS:
public partial class Toolbar : UserControl
{
public Toolbar()
{
InitializeComponent();
}
// Using a DependencyProperty as the backing store for CommandTarget. This enables animation, styling, binding, etc...
public …Run Code Online (Sandbox Code Playgroud)