C# UserControl中的RoutedCommand未按预期工作
我正试图在我的C# UserControl中的RoutedCommand未按预期工作,c#,wpf,user-controls,command,routed-commands,C#,Wpf,User Controls,Command,Routed Commands,我正试图在我的UserControls中使用RoutedCommands,灵感来自Josh Smith的这篇文章 我的做法与本文中的示例略有不同,并在Usercontrol中定义了RoutedCommand和CommandBindings 现在我试着把它用在我的主窗口上。因此,通过单击按钮,可以执行UserControl中的命令。不幸的是,我得到的是一个禁用的按钮 永远不会执行Foo\u CanExecute()方法 <UserControl x:Class="RoutedCommand
UserControls
中使用RoutedCommands
,灵感来自Josh Smith的这篇文章
我的做法与本文中的示例略有不同,并在Usercontrol中定义了RoutedCommand和CommandBindings
现在我试着把它用在我的主窗口上。因此,通过单击按钮,可以执行UserControl中的命令。不幸的是,我得到的是一个禁用的按钮
永远不会执行Foo\u 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>
在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>
您的命令位于用户控件中,而按钮位于主窗口中 其中可能包含您的用户控件 比如冒泡和路由事件(用于驱动它们) Executed查找从可视树冒泡到绑定的命令 PreviewExecuted查找沿可视树向下挖掘到绑定的命令 由于您的按钮位于usercontrol的父控件中,我不确定冒泡还是隧道都会起作用 但是,隧道挖掘将是预演式的,预演式的 Routedcommands要正确执行可能非常棘手 有时需要做的一件事是绑定commandtarget,告诉它去哪里看 例如:
对我有用
我很少发现它们有用——这是一个方面,使它们没有你最初想象的那么有用
编辑:
也许值得一提的是,与常规的icommand相比,另一件事使这些命令不具吸引力。您需要使用静态的,这意味着它只适用于非常通用的命令,或者您需要将在代码隐藏中的事件处理程序
另一方面
如果你在写一些东西,你就必须对任何有焦点的东西进行一般性的处理。比如说一个有多个文本框的文本编辑器,你在做文本操作。路由命令可能是合适的。不过,在我开发的应用程序中,我从未遇到过这样的要求。我用完整的(尚未运行)代码更新了问题。我仍然有问题。该按钮是ViewControl的同级。他们都是Window的孩子。仍然不起作用。我认为您需要在该命令上使用commandtarget。(更新答案)。谢谢你,安迪!你让我开心!它起作用了!你还记得我今天早些时候的另一个问题吗?使用这种方法,我可以在没有任何代码的情况下完成这项工作!
<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>
<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>