将焦点设置为控件模板内的子元素

时间:2014-11-20 09:58:55

标签: c# wpf xaml controltemplate

我有一个控件模板,用于从扩展WPF工具包的CheckComboBox控件派生的控件(与标准ComboBox几乎相同)。我正在尝试跟踪何时按下此控件上的键,同时打开其下拉部分以修改其某些属性。但是,当我打开下拉列表并按任意键时,没有任何事情发生(断点既不在OnKeyDown内也不在OnPreviewKeyDown内部覆盖方法中。只有当我之前点击下拉部分内的控件时,它们才会被击中。因此,即使子控件没有焦点,我怎么能让它们工作呢?当下拉打开时,如何将焦点设置为子控件。对于后者,我尝试在ControlTemplate.Triggers集合中使用触发器,但它无法使用{x:Reference ChildControl}{Binding ElementName=ChildControl}找到子元素。

以下是模板

的XAML代码段
<ControlTemplate x:Key="MyTemplate" TargetType="{x:Type wpfApplication2:DropDownMultiSelector}">
        <ControlTemplate.Triggers>
            <!--This one doesn't work-->
            <Trigger Property="IsDropDownOpen" Value="True">
                <Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=PART_Popup}" />
            </Trigger>
        </ControlTemplate.Triggers>
        <Grid x:Name="MainGrid" SnapsToDevicePixels="True">
            <Popup x:Name="PART_Popup" AllowsTransparency="True" IsOpen="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom" StaysOpen="False">
                <Grid MinWidth="{Binding ActualWidth, ElementName=MainGrid}">
                    <Border x:Name="DropDownBorder" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" MaxHeight="{Binding MaxDropDownHeight, RelativeSource={RelativeSource TemplatedParent}}">
                        <ScrollViewer x:Name="DropDownScrollViewer">
                            <Grid RenderOptions.ClearTypeHint="Enabled">
                                <Canvas HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
                                    <Rectangle x:Name="OpaqueRect" Fill="{Binding Background, ElementName=DropDownBorder}" Height="{Binding ActualHeight, ElementName=DropDownBorder}" Width="{Binding ActualWidth, ElementName=DropDownBorder}"/>
                                </Canvas>
                                <ContentControl x:Name="ChildControl" Content="{TemplateBinding ChildItemsControl}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                            </Grid>
                        </ScrollViewer>
                    </Border>
                </Grid>
            </Popup>
            ...

2 个答案:

答案 0 :(得分:1)

帮助我的是设定注意力来控制自己,而不是控制它。

<ControlTemplate.Triggers>
    <Trigger Property="IsDropDownOpen" Value="True">
        <Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Self}}" />
    </Trigger>
</ControlTemplate.Triggers>

答案 1 :(得分:0)

听取Loaded事件并设置焦点。

以下是如何在XAML中处理事件的示例:)

<Grid>
    <!–Define the button content –>
    <Button Width="133" Height="24" Name="btnExitApp" Click="btnExitApp_Clicked" Content="Exit Application"/>
    <!–The C# implementation of the button's Click event handler. –>
    <x:Code>
        <![CDATA[            
            private void btnExitApp_Clicked(object sender, RoutedEventArgs e)
            {
                // Get a handle to the current app and shut it down.
                Application.Current.Shutdown();
            }
        ]]>
    </x:Code>
</Grid>

是的,您可以在XAML中编写代码! :)