【问题标题】:WPF UserControl FocusWPF 用户控件焦点
【发布时间】:2011-09-29 23:09:22
【问题描述】:

我有一个UserControl 子类,其中包含一个Grid,而该子类又包含一对TextBlocks 和一个Border(其中还包含一个TextBlock)。请参阅下面的代码。

    <UserControl x:Class="ProjectNS.MyUserControl"
       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:my="clr-namespace:ProjectNS"
       mc:Ignorable="d" 
       Height="49" Width="150" BottomResizeLocked="True" TopResizeLocked="True"  
       MoveLocks="Vertical" Margin="0,-4" Focusable="True">
        <my:MyUserControl.Resources>
            <Style x:Key="BorderStyle" TargetType="Border">
                <Setter Property="Background" Value="Blue"/>
                <Setter Property="BorderBrush" Value="Blue"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource 
                     AncestorType={x:Type my:GanttBar}}, Path=IsKeyboardFocusWithin}" 
                     Value="True">
                        <Setter Property="Background" Value="{StaticResource 
                        SelectedGanttBarBackGroundBrush}"/>
                        <Setter Property="BorderBrush" Value="{StaticResource 
                        SelectedGanttBarBorderBrush}"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </my:MyUserControl.Resources>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="2*" />
                <RowDefinition Height="3*" />
                <RowDefinition Height="2*" />
            </Grid.RowDefinitions>
            <Label FontSize="8.5" HorizontalAlignment="Left"
            VerticalAlignment="Bottom" Margin="4,0,0,0" Foreground="Green" />
            <Border Grid.Row="1" CornerRadius="5" BorderThickness="1.5" Style="
            {StaticResource BorderStyle}" FocusVisualStyle="{StaticResource 
             SelectedBorderStyle}" Focusable="True" >
                <Label FontSize="10" HorizontalAlignment="Center" FontWeight="Bold" 
                 VerticalAlignment="Top" Foreground="White" Margin="0,2,0,0" />
            </Border>
            <Label HorizontalAlignment="Right" FontSize="8.5" Grid.Row="2" 
            VerticalAlignment="Top" Margin="0,0,4,0" Foreground="Red" />
        </Grid>
    </my:MyUserControl>

当我的UserControl 获得焦点时,我试图让嵌入边框的颜色改变颜色,但我无法终生弄清楚当我单击控件时实际获得焦点的是什么。我已经尝试为每个控件使用GotFocus 事件,一旦程序运行,什么都不会触发。

【问题讨论】:

    标签: wpf user-controls focus


    【解决方案1】:

    在属性 IsKeyboardFocusWithin 上使用 DataTrigger。我不确定确切的语法,但它应该看起来像这样:

    <Style TargetType="{x:Type Border}">
        <Setter Property="BorderBrush" Value="Red" />
        <Style.Triggers>
    
            <!-- Will change Border color when KeyboardFocus inside Border -->
            <Trigger Property="IsKeyboardFocusWithin" Value="True">
                <Setter Property="BorderBrush" Value="Green" />
            </Trigger>
    
            <!-- Will change Border color when UserControl contains KeyboardFocus -->
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource 
                AncestorType={x:Type local:MyUserControl}}, 
                Path=IsKeyboardFocusWithin}" Value="True">
                <Setter Property="BorderBrush" Value="Blue" />
            </DataTrigger>
    
        </Style.Triggers>
    </Style>
    

    【讨论】:

    • 我什至不知道什么是焦点(UserControlTextBlocks 或 Border)。如果Border 得到关注,那么我同意这应该可行。如果Border 没有获得焦点,当UserControl 中的其他内容获得焦点时,我该如何更改其样式?
    • @kungfumath IsKeyboardFocusWithin 如果焦点位于边界内的任何控件内,则返回 true。我不能 100% 确定边框是否具有该属性,但如果没有,您可以使用 RelativeSource 绑定来查看 UserControl.IsKeyboardFocusWithin 是否为真,如果您的 UserControl 中的任何控件具有焦点,它将返回真。
    • @kungfumath 我将两个绑定都添加到我的答案中,并测试了两者以确保它们工作
    • 好的,现在我收到一个运行时错误,提示我无法将绑定应用到 Trigger.Property。我看到你的代码......正在尝试。
    • @kungfumath 普通的Trigger 不能使用绑定。它旨在与当前样式对象上存在的属性一起使用。如果要在触发器中使用绑定,则需要使用DataTrigger(请参阅答案中的第二个触发器)
    猜你喜欢
    • 2014-02-09
    • 1970-01-01
    • 1970-01-01
    • 2014-02-20
    • 1970-01-01
    • 1970-01-01
    • 2012-10-22
    • 2011-01-23
    • 2011-07-17
    相关资源
    最近更新 更多