为什么DataBinding没有传播到UserControl

时间:2016-10-03 20:58:44

标签: c# wpf user-controls datatemplate commandbinding

今天早上我问了一个问题here,做一个简单的工作样本给了我一个与预期不同的行为。

GitHub处的完整工作样本。主要部分代码如下。

在本例中,如果UserControl直接用作Window的子节点,则命令永远不会传播到任何UserControl。如果UserControl用作ListBox ItemTemplate的DataTemplate,它也不起作用。

我还包含一个黑客按钮来修复Command到达UserControls的问题。黑客来自StackOverflow

但是使用hack并没有解释为什么UserControl没有收到命令(没有它)并且使用这个hack也打破了良好编码的第一条规则:“Hi cohesion and Low coupling”。应该在窗口代码中使用hack来管理UserControl中的Command,我的想法是它应该默认发生。

为什么命令不会默认传播到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:

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

1 个答案:

答案 0 :(得分:1)

您没有在用户控件上调用命令的原因是您的按钮不在单独的焦点范围内。为了让WPF正确地为命令目标选择聚焦元素,它需要在命令调用控制的单独焦点范围内。

框架将从按钮遍历可视树,在其焦点范围内查找命令绑定(在您的情况下,它将找不到任何)。当框架在当前焦点范围内找不到任何命令绑定时,它才会查看聚焦元素的父焦点范围(在您的情况下,按钮位于Window焦点范围内,没有父范围,因此搜索将在那里结束)。

只需在FocusManager.IsFocusScope="True"上设置StackPanel即可解决问题。

您还可以在按钮上指定CommandTarget属性以指向您的用户控件,而不是依赖于焦点。

相关问题