1

I have what to some may seem like a simple problem and despite my best efforts, I have been unable to 'fix' this problem:

I have a custom style for a TextBox, defined in a 'common' assembly; this style overrides the default and is thus used every time someone uses a TextBox. The problem is that when the style is applied, the GotFocus, IsKeyboardFocusWithinChanged and PreviewGotKeyboardFocus events are -not- fired any longer. I've verified that this only happens when this style is applied (If I comment it out and run the application, the events are fired correctly).

So my question is essentially, has anyone experienced anything similar? And if so, does anyone know of a solution for this problem?

The style is as follows (The static resources a simple SolidColorBrushes):

<Style TargetType="{x:Type TextBox}" x:Key="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}"> <Setter Property="FocusVisualStyle" Value="{x:Null}" /> <Setter Property="OverridesDefaultStyle" Value="True" /> <Setter Property="BorderBrush" Value="{StaticResource BrushBorder}"/> <Setter Property="ContextMenu" Value="{StaticResource TextBoxContextMenu}" /> <Setter Property="Foreground" Value="{StaticResource BrushForeground}" /> <Setter Property="SelectionBrush" Value="{StaticResource BrushHighlight}" /> <Setter Property="Background" Value="{StaticResource BrushBackground}"/> <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/> <Setter Property="SnapsToDevicePixels" Value="True"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type TextBox}"> <Border BorderThickness="1" x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}"> <ScrollViewer x:Name="PART_ContentHost" Margin="0" /> </Border> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="IsKeyboardFocusWithin" Value="True"> <Setter Property="Background" Value="{StaticResource BrushLightBg}" /> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="{StaticResource BrushMouseoverBackground}" /> </Trigger> </Style.Triggers> </Style> 

1 Answer 1

2

Hi thi is working just fine here, I had to add some colors you were missing, which will make your eyes bleed pardon that, but I'm in a bit of a rush here :)

<!-- nice colors you were missing them ;) --> <SolidColorBrush x:Key="BrushBorder" Color="Blue"/> <SolidColorBrush x:Key="TextBoxContextMenu" Color="DeepPink"/> <SolidColorBrush x:Key="BrushForeground" Color="Red"/> <SolidColorBrush x:Key="BrushHighlight" Color="Aqua"/> <SolidColorBrush x:Key="BrushBackground" Color="DarkBlue"/> <SolidColorBrush x:Key="BrushLightBg" Color="LightSeaGreen"/> <SolidColorBrush x:Key="BrushMouseoverBackground" Color="Yellow"/> <Style TargetType="{x:Type TextBox}" x:Key="TextBoxStyleMessed" BasedOn="{StaticResource {x:Type TextBox}}" > <Setter Property="FocusVisualStyle" Value="{x:Null}" /> <Setter Property="OverridesDefaultStyle" Value="True" /> <Setter Property="BorderBrush" Value="{StaticResource BrushBorder}"/> <!--<Setter Property="ContextMenu" Value="{StaticResource TextBoxContextMenu}" />--> <Setter Property="Foreground" Value="{StaticResource BrushForeground}" /> <Setter Property="SelectionBrush" Value="{StaticResource BrushHighlight}" /> <Setter Property="Background" Value="{StaticResource BrushBackground}"/> <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/> <Setter Property="SnapsToDevicePixels" Value="True"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type TextBox}"> <Border BorderThickness="1" x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}"> <ScrollViewer x:Name="PART_ContentHost" Margin="0" /> </Border> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="IsKeyboardFocusWithin" Value="True"> <Setter Property="Background" Value="{StaticResource BrushLightBg}" /> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="{StaticResource BrushMouseoverBackground}" /> </Trigger> </Style.Triggers> </Style> ..... <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <TextBox Text="Hello!" HorizontalAlignment="Center" VerticalAlignment="Center" Style="{DynamicResource TextBoxStyleMessed}" x:Name="HateThisNonMVVMStuff"/> <Button Grid.Row="1">Hodwy</Button> </Grid> 

Had to cut some corners but nasty codebehind:

public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); Loaded += MainWindow_Loaded; } void MainWindow_Loaded(object sender, RoutedEventArgs e) { HateThisNonMVVMStuff.PreviewLostKeyboardFocus += HateThisNonMVVMStuff_PreviewLostKeyboardFocus; HateThisNonMVVMStuff.LostFocus += UIElement_OnLostFocus; HateThisNonMVVMStuff.GotFocus += UIElement_OnGotFocus; } void HateThisNonMVVMStuff_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) { System.Diagnostics.Trace.WriteLine("PrevLostFocus!"); } private void UIElement_OnGotFocus(object sender, RoutedEventArgs e) { System.Diagnostics.Trace.WriteLine("Hei! Gained focus"); } private void UIElement_OnLostFocus(object sender, RoutedEventArgs e) { System.Diagnostics.Trace.WriteLine("Hei! Lost focus"); } } 

The events fires with your style, I applied it like a style for this one box only. I got an exception on your contextmenu and that it wouldn't have my nasty color, but hey, who would! :D

Hope it helps,

Cheers,

Stian

Sign up to request clarification or add additional context in comments.

6 Comments

So you -are- getting the Focus events I mentioned? Where exactly are you adding handlers for those? I'm adding them through a separate behaviour class, and they're working just fine without the style - but not WITH the style.
@MelanieSchmidt added them in nasty codebehind there, had to cut som corners. but they fire at will here. Forgot to paste it in.
Okay this is really strange - they're now working for me as well... Must've been a fluke... Marking your answer as solution, thanks for your time!
Promise me you make a viewmodel and use eventocommand for removing the nasty codebehind! ;) I got some exception with the contextmenu, that's why I remmed it out. But I don't have time to check if this was the problem. It might actually been just vs for all I know.
The style is used from within a view, which is bound to a viewmodel - the only thing the events are used for is to 'select all' (And this is done from a separate Behaviour class) - Such that the xaml code for a TextBox in my app would look something like: <TextBox TextSelectionBehaviour.AutoSelectOnFocus="True" Text="{Binding Whatever}"/> - Don't worry, there's NO code behind apart from the auto-generated stuff :P
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.