UserControl中的RoutedCommand无法正常工作 [英] RoutedCommand in UserControl is not working as expected

查看:115
本文介绍了UserControl中的RoutedCommand无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图根据乔希·史密斯(Josh Smith)的这篇文章,在我的UserControls中使用RoutedCommands.

I am trying to use RoutedCommands in my UserControls inspired by this article from Josh Smith.

> https://joshsmithonwpf.wordpress.com/2008/03/18/understanding-routed-commands/

我做了一些与本文示例不同的操作,并在Usercontrol中定义了RoutedCommand和CommandBindings.

I did it a bit different from the example in the article and defined the RoutedCommand and CommandBindings in the Usercontrol.

现在我正尝试在MainWindow中使用它.这样,通过单击按钮,将执行UserControl中的Command.不幸的是,我得到的是一个禁用的按钮.

Now I am trying to use it my MainWindow. So that by clicking the Button, the Command in UserControl is executed. Unfortunately what I get is a disabled button.

Foo_CanExecute()方法从不执行.

<UserControl x:Class="RoutedCommandTest.ViewControl"
             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:RoutedCommandTest"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <UserControl.CommandBindings>
        <CommandBinding
            Command="{x:Static local:ViewControl.Foo}"
            PreviewCanExecute="Foo_CanExecute"
            PreviewExecuted="Foo_Executed"
        />
    </UserControl.CommandBindings>
    <Grid>

    </Grid>
</UserControl>

在ViewControl.xaml.cs

In ViewControl.xaml.cs

    public static readonly RoutedCommand Foo = new RoutedCommand();

    void Foo_CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = true;
    }

    void Foo_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        MessageBox.Show("The Window is Fooing...");
    }

    public ViewControl()
    {
        InitializeComponent();
    }

在MainWindow.xaml中:

In MainWindow.xaml:

<Window x:Class="RoutedCommandTest.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:RoutedCommandTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <local:ViewControl/>
        <Button Content="Foo" Margin="0 5" Command="{x:Static local:ViewControl.Foo}"/>
    </Grid>
</Window>

推荐答案

您的命令位于用户控件中,而按钮位于主窗口中.

Your command is in a usercontrol, whilst the button is in mainwindow.

大概包含您的用户控件.

Which presumably contains your usercontrol.

像冒泡事件和路由事件(用于驱动事件).

Like bubbling and routing events ( which are used to drive them ).

Executed查找将可视树冒泡到绑定的命令.

Executed looks for the command bubbling UP the visual tree to the binding.

PreviewExecuted查找将可视树向下绑定到绑定的命令.

PreviewExecuted looks for the command tunnelling DOWN the visual tree to the binding.

由于您的按钮位于用户控件的父级中,因此我不确定冒泡还是隧穿都可以.

Since your button is in the parent of the usercontrol I'm not sure whether either bubbling or tunnelling will work.

但是隧道将被PreviewExecute和PreviewCanExecute.

But tunnelling would be PreviewExecuted And PreviewCanExecute.

https ://docs.microsoft.com/zh-cn/dotnet/api/system.windows.input.commandbinding.previewexected?view = netframework-4.8

https ://docs.microsoft.com/zh-cn/dotnet/api/system.windows.input.commandbinding.previewcanexecute?view = netframework-4.8

正确执行路由命令可能很棘手.

Routedcommands can be pretty tricky to get right.

有时候您需要做的一件事就是绑定commandtarget告诉它去哪里.

One thing you sometimes have to do is to bind commandtarget to tell it where to go look.

例如:

    <Grid>
      <local:UserControl1 x:Name="UC1" Height="60" Width="100"/>
      <Button Content="Foo" TextElement.FontSize="30" Command="{x:Static local:UserControl1.Foo}"
              CommandTarget="{Binding ElementName=UC1}"
              />
    </Grid> 

为我工作.

我很少发现它们有用-这是使它们不如您最初想象的有用的方面之一.

I have rarely found them useful - this is one of the aspects makes them way less useful than you might at first imagine.

这篇关于UserControl中的RoutedCommand无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆