Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlight a WPF control when it gets focus

I am working on a WPF application having lots of screens and every screen has lots of controls on it. It becames very difficult to determine which control has focus.

So i want to highlight the control that currently have focus on it. It could be textbox, button, combobox, listbox or grid.

It'll be better if we could do this using styles and triggers.

Thanks

like image 427
Manvinder Avatar asked Jun 14 '12 05:06

Manvinder


1 Answers

You should use a trigger for the right event. In your case it's IsFocused. Simple example:

<Trigger Property="IsFocused" Value="true">
    <Setter Property="BorderBrush" Value="Red" />
    <Setter Property="BorderThickness" Value="1" />
</Trigger>

You should use a Style tag, if you want to apply this style for a particular control type (use TargetType="{x:Type TextBox}", for example).

If you want to apply to all control types in your application, than you should consider using a base style with only such kind of trigger and than just inherit it in your custom styles with a help of BasedOn attribute: <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource BaseFocusStyle}">

Good example of how to do this is a SO question: "How to make Style.Triggers trigger a different named style to be applied".

like image 143
Sergei Danielian Avatar answered Oct 02 '22 14:10

Sergei Danielian