Why IsFocused not fired on Label
I just want to define a trigger that changes a Label background color when focused, but it doesn't work. Doing the same with button is OK. Are there something wrong ?!?! I also got a same problem with Border and textblock.
Update code xaml:
<Window.Resources>
<SolidColorBrush x:Key="GridLineBrush" Color="#8DAED9" />
<SolidColorBrush x:Key="HeaderWeekDayBackground" Color="#A5BFE1" />
<Style x:Key="borderStyle" TargetType="Control">
<Setter Property="Background" Value="{StaticResource HeaderWeekDayBackground}" />
<Style.Triggers>
<Trigger Property="IsFocused" Value="true">
<Setter Prop开发者_StackOverflow中文版erty="Background" Value="Blue" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Style="{StaticResource borderStyle}"
Grid.Row="0" >
</Button>
<Label Focusable="True" Style="{StaticResource borderStyle}"
Grid.Row="1" >
</Label>
</Grid>
</Window>
Not all controls are focusable by default, set Focusable
to true
ans see if that helps.
One problem you might encounter is that by default the Label does not receive focus from mouse-events.
I do not know if there is a clean XAML-only way to set the focus but you could handle a mouse-event:
<Label Focusable="True" Content="Test" MouseLeftButtonUp="Label_MouseLeftButtonUp">
<Label.Style>
<Style TargetType="Label">
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Background" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>
//Note that this is not a "proper" click.
private void Label_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
var label = sender as Label;
label.Focus();
}
精彩评论