今天早上我问了一个问题here https://stackoverflow.com/questions/39835222/command-binding-is-not-propagating-for-control-into-a-datatemplate-of-itemtempla?noredirect=1#comment66965701_39835222做一个简单的工作示例给了我与预期不同的行为。
完整工作样本位于GitHub https://github.com/EricOuellet2/CommandRoutingIntoItemTemplate。主要部分代码如下。
在本例中,命令永远不会传播到任何 UserControl,无论 UserControl 直接用作 Window 的子项。如果将 UserControl 用作 ListBox ItemTemplate 的 DataTemplate,它也不起作用。
我还包括一个 hack 按钮来解决命令到达用户控件的问题。黑客来自堆栈溢出 https://stackoverflow.com/questions/24099274/menuitem-in-window-commandbinding-in-usercontrol.
但是使用这个 hack 并不能解释为什么 UserControl 没有收到命令(没有它),并且使用这个 hack 也打破了良好编码的第一条规则:“高内聚和低耦合”。应该在窗口代码中使用 hack,以便它能够管理 UserControl 中的命令,我的想法是默认情况下应该发生这种情况。
为什么命令默认情况下不传播到 UserControl,我应该如何做才能以干净的方式将命令传播到 UserControl?
注意:在 UserControl 中仅使用一个 CommandBinding(删除其中一个)并不能解决问题。
部分代码:
<Window x:Class="CommandRoutingIntoItemTemplate.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:CommandRoutingIntoItemTemplate"
xmlns:system="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<local:UserControlTest></local:UserControlTest>
<ListBox Grid.Row="1">
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Aqua" BorderThickness="2">
<local:UserControlTest></local:UserControlTest>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.Items>
<system:String>1</system:String>
<system:String>2</system:String>
</ListBox.Items>
</ListBox>
<StackPanel Grid.Row="2" Orientation="Horizontal">
<Button Command="local:Commands.CommandTest">Put focus on TestBlock and click here to see if command occurs</Button>
<Button Click="AddHack">Hack</Button>
</StackPanel>
</Grid>
</Window>
用户控制:
<UserControl x:Class="CommandRoutingIntoItemTemplate.UserControlTest"
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:CommandRoutingIntoItemTemplate"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.CommandBindings>
<CommandBinding Command="local:Commands.CommandTest" CanExecute="CommandTestCanExecuteUserControl" Executed="CommandTestExecuteUserControl"></CommandBinding>
</UserControl.CommandBindings>
<Grid>
<TextBox Text="UserControlTest">
<TextBox.CommandBindings>
<CommandBinding Command="local:Commands.CommandTest" CanExecute="CommandTestCanExecuteTextBox" Executed="CommandTestExecuteTextBox"></CommandBinding>
</TextBox.CommandBindings>
</TextBox>
</Grid>
</UserControl>