WPF: How to set a Label style when the target TextBox has focus?
I would like to highlight a label when the associated textbox has focus. This works:
<Label Grid.Row="1" Grid.Column="0" Target="{Binding ElementName=CountryCode}">
<Label.Style>
<Style TargetType="{x:Type Label}">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=CountryCode, Path=(IsFocused)}" Value="True">
<Setter Property="Foreground" Value="Blue" />
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
<AccessText Text="{Binding Path=CountryCodeLabel}" />
</Label>
<TextBox Grid.Row="1" Grid.Column="1" Name="CountryCode" Text="{Binding Path=CountryC开发者_运维知识库ode}" />
But I have a bunch of these textboxes, so I'd prefer to template the style. This works:
<Style x:Key="HighlightOnFocus" TargetType="{x:Type Label}">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=CountryCode, Path=(IsFocused)}" Value="True">
<Setter Property="Foreground" Value="Blue" />
</DataTrigger>
</Style.Triggers>
</Style>
...
<Label Grid.Row="1" Grid.Column="0" Style="{StaticResource HighlightOnFocus}">
<AccessText Text="{Binding Path=CountryCodeLabel}" />
</Label>
<TextBox Grid.Row="1" Grid.Column="1" Name="CountryCode" Text="{Binding Path=CountryCode}" />
but of course I can't hard code the ElementName in there. So I tried this:
<Style x:Key="HighlightOnFocus" TargetType="{x:Type Label}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=(IsFocused)}" Value="True">
<Setter Property="Foreground" Value="Blue" />
</DataTrigger>
</Style.Triggers>
</Style>
...
<Label Grid.Row="1" Grid.Column="0" Style="{StaticResource HighlightOnFocus}" DataContext="{Binding ElementName=CountryCode}">
<AccessText Text="{Binding Path=CountryCodeLabel}" />
</Label>
<TextBox Grid.Row="1" Grid.Column="1" Name="CountryCode" Text="{Binding Path=CountryCode}" />
But setting the DataContext in the label messes up the binding in my AccessText element. So the question is - is there a way specify the element name for the style datatrigger in some way other than setting the datacontext? Is there a better way to accomplish what I'm trying to do?
There is a nice solution over here: https://social.msdn.microsoft.com/Forums/vstudio/en-US/627e4d27-351b-4e2d-86a3-9367af3f0edf/how-to-set-a-label-style-when-the-target-textbox-has-focus?forum=wpf
In general, it's fairly easy. Create a custom attached property like this:
public static class CustomAttached
{
public static readonly DependencyProperty IsTargetFocusedProperty = DependencyProperty.RegisterAttached(
"IsTargetFocused", typeof(bool), typeof(CustomAttached),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender)
);
public static void SetIsTargetFocused(UIElement element, bool value)
{
element.SetValue(IsTargetFocusedProperty, value);
}
public static bool GetIsTargetFocused(UIElement element)
{
return (bool)element.GetValue(IsTargetFocusedProperty);
}
}
Then add a style for the highlighting:
<Window.Resources>
<Style x:Key="HighlightOnFocus" TargetType="{x:Type Label}">
<Style.Triggers>
<Trigger Property="local:CustomAttached.IsTargetFocused" Value="True">
<Setter Property="Background" Value="CadetBlue" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
And then simply use it like that:
<Label Content="Label" Style="{StaticResource HighlightOnFocus}"
local:CustomAttached.IsTargetFocused="{Binding IsFocused, ElementName=textBox}">
</Label>
<TextBox x:Name="textBox" Text="TextBox" />
精彩评论