RoutedCommand in UserControl is not working as expected

耗尽温柔 提交于 2019-12-25 01:13:54

问题


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/

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

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.

The Foo_CanExecute() method is never executed.

<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>

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();
    }

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>

回答1:


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 looks for the command bubbling UP the visual tree to the binding.

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.

But tunnelling would be PreviewExecuted And PreviewCanExecute.

https://docs.microsoft.com/en-us/dotnet/api/system.windows.input.commandbinding.previewexecuted?view=netframework-4.8

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

Routedcommands can be pretty tricky to get right.

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

eg:

    <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> 

Works for me.

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



来源:https://stackoverflow.com/questions/56315317/routedcommand-in-usercontrol-is-not-working-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!