WPF用户控制焦点
我有一个WPF用户控制焦点,wpf,user-controls,focus,Wpf,User Controls,Focus,我有一个UserControl子类,它包含一个Grid,它依次包含一对TextBlocks和一个Border(它还包含一个TextBlock)。请参阅下面的代码 <UserControl x:Class="ProjectNS.MyUserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.c
UserControl
子类,它包含一个Grid
,它依次包含一对TextBlock
s和一个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
事件,程序运行后不会触发任何事件。在属性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>
我甚至不知道什么是焦点(用户控件、任何文本块或边框)。如果边框
得到了关注,那么我同意这应该是可行的。如果边框
未获得焦点,当用户控件
中的其他内容获得焦点时,如何更改其样式?@kungfumathIsKeyboardFocusWithin
将在焦点位于边框内的任何控件内时返回true。我不能100%确定borders是否具有该属性,但如果没有,您可以使用RelativeSource
绑定来查看UserControl.IsKeyboardFocusWithin
是否为true,如果UserControl中的任何控件具有焦点,它将返回true。@kungfumath我将这两个绑定添加到我的答案中,并对它们进行了测试,以确保它们都能正常工作,现在,我得到一个运行时错误,该错误表示我无法将绑定应用于Trigger.Property
。我看到你的代码了。。。正在尝试。@kungfumath常规触发器无法使用绑定。它用于当前样式化对象上存在的属性。如果要在触发器中使用绑定,则需要使用DataTrigger
(请参阅答案中的第二个触发器)