I have a Window that has two primary areas. One is a TextBox inside of a ScrollViewer and the other is a TabControl. I want to have a red border around the part that currently has focus, so I wrote the following code to do that
Xaml
<ScrollViewer BorderBrush="Red"
BorderThickness="0"
GotFocus="Border_GotFocus"
LostFocus="Border_LostFocus">
<TextBox/>
</ScrollViewer>
<TabControl BorderBrush="Red"
BorderThickness="0"
GotFocus="Border_GotFocus"
LostFocus="Border_LostFocus">
</TabControl>
Code
private void Border_LostFocus(object sender, RoutedEventArgs e)
{
var control = sender as Control;
if (control != null)
{
control.BorderThickness = new Thickness(0);
}
}
private void Border_GotFocus(object sender, RoutedEventArgs e)
{
var control = sender as Control;
if (control != null)
{
control.BorderThickness = new Thickness(2);
}
}
The problem is that if I click on the TextBox it does not update the border around the ScrollViewer. If I click on a Tab in the TabControl it updates the border so that I can see the border, but doesn't "remove" it when I click somewhere else. Is there some better way to do this?