由子元素绑定的RoutedCommands永远不会触发

时间:2017-10-04 10:09:08

标签: wpf xaml routed-commands routedcommand

我正试图在WPF中了解RoutedCommands。我喜欢它们如何减少不同UI元素和模型之间的耦合,但我似乎无法使绑定适用于作为窗口子项的自定义控件。我想这对你们任何一个WPF巫师来说都是一些轻松的信誉! : - )

以下是一些可以尝试的示例代码:

路由命令:

using System.Windows.Input;

namespace Wpf.RoutedCommands
{
    public static class Commands
    {
        public static readonly RoutedCommand SayHi = new RoutedCommand();
    }
}

主窗口XAML:

<Window x:Class="Wpf.RoutedCommands.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Wpf.RoutedCommands"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<!-- uncommenting the below makes it work but introduces coupling -->
<!--<Window.CommandBindings>
    <CommandBinding Command="{x:Static local:Commands.SayHi}" Executed="cmdSayHi" CanExecute="cmdCanSayHi"></CommandBinding>
</Window.CommandBindings>-->
<DockPanel LastChildFill="True">
    <Button DockPanel.Dock="Bottom" Content="Say Hi!" Command="{x:Static local:Commands.SayHi}" />
    <local:Greeter x:Name="Greeter" />
</DockPanel>    

主窗口代码:

using System.Windows.Input;

namespace Wpf.RoutedCommands
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        // these two will be used if you uncomment the command bindings in XAML
        private void cmdCanSayHi(object sender, CanExecuteRoutedEventArgs e) => e.CanExecute = true;

        private void cmdSayHi(object sender, ExecutedRoutedEventArgs e) => Greeter.SayHi();
    }
}

自定义控件(“Greeter”)XAML:

<UserControl x:Class="Wpf.RoutedCommands.Greeter"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:Wpf.RoutedCommands"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<UserControl.CommandBindings>
    <CommandBinding Command="{x:Static local:Commands.SayHi}" Executed="cmdSayHi"  CanExecute="cmdCanSayHi"/>
</UserControl.CommandBindings>
<Grid>
    <Label x:Name="Label" />
</Grid>

Greeter代码:

using System.Windows.Input;

namespace Wpf.RoutedCommands
{
    public partial class Greeter
    {
        public Greeter()
        {
            InitializeComponent();
        }

        private void cmdSayHi(object sender, ExecutedRoutedEventArgs e) => SayHi();

        private void cmdCanSayHi(object sender, CanExecuteRoutedEventArgs e) => e.CanExecute = true;

        public void SayHi() => Label.Content = "Hi!";
    }
}

如果您创建一个WPF项目并使用上面的内容,您将看到主窗口中的按钮被禁用。调试它表明永远不会调用Greeter.cmdCanSayHi方法。

如果在主窗口中取消注释休眠XAML,一切正常。那么:为什么我可以从窗口绑定命令而不是从子控件绑定?是与渲染时间有关吗?

1 个答案:

答案 0 :(得分:1)

RoutedCommand从焦点元素中搜索可视树,向上搜索具有匹配CommandBinding的元素,然后为此执行Execute委托特别是CommandBinding

您的UserControl位于位于元素树中的Button