I got a CustomControl inherit from : Control. It contains 2 Labels, 1 TextBox and 2 Buttons (simplyfied view)
<Button IsTabStop="False" x:Name="ParameterUpButton" IsEnabled="False" Width="14" Height="10">
<Button IsTabStop="False" x:Name="ParameterDownButton" IsEnabled="False" Width="14" Height="10"/>
<StackPanel Orientation="Horizontal" Height="28" VerticalAlignment="Center">
<Label Content="{TemplateBinding ParameterName}" Width="45" />
<TextBox IsEnabled="False" x:Name="ParameterValueTextBox" Text="{TemplateBinding ParameterValue}" Width="36" Height="23"/>
<Label Content="{TemplateBinding ParameterUnit}" Width="33" />
</StackPanel>
As the user should be able to navigate through some of these custom controls, i want to redirect the Focus by tabbing through my controls. Simply said:
I want to navigate through all these Textboxes, and only those (the buttons should be no tab stops!).
The Buttons and the Textbox are disabled by default, and the template got some triggers to enable them by IsMouseOver and by IsKeyboardFocused (to show a different style).
How can i focus the Textbox (and enable it including the two buttons)
- using Tab to navigate through my controls?
- by clicking on my control (no matter where, just within the control)
The usage looks like the following:
<!-- some more controls -->
<CustomControls:ParameterControl ParameterName="foo" ParameterUnit="x" ParameterValue="6" Margin="5"/>
<CustomControls:ParameterControl ParameterName="bar" ParameterUnit="y" ParameterValue="6" Margin="5"/>
<CustomControls:ParameterControl ParameterName="foobar" ParameterUnit="z" ParameterValue="6" Margin="5"/>
<!-- some more controls -->
Thanks for any help in advance!
Edit: I tried it by overriding the Control’s OnGotFocus event like this:
protected override void OnGotFocus(RoutedEventArgs e)
{
var tb = ((TextBox) Template.FindName("ParameterValueTextBox", this));
tb.IsEnabled = true;
tb.Focus();
}
I also wrote a Trigger for my Control:
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition SourceName="ParameterValueTextBox" Property="IsKeyboardFocused" Value="true"/>
</MultiTrigger.Conditions>
<MultiTrigger.Setters>
<Setter Property="IsEnabled" TargetName="ParameterDownButton" Value="true"/>
<Setter Property="IsEnabled" TargetName="ParameterUpButton" Value="true"/>
<Setter Property="IsEnabled" TargetName="ParameterValueTextBox" Value="true"/>
</MultiTrigger.Setters>
</MultiTrigger>
Actually, this trigger works for the Buttons (i can see the style changing and changing back after losing the focus), but my TextBox is still being enabled (at least the style does not change back)
Meanwhile, i’ve found the solution myself. Might be interesting to somebody else:
As i’ve been juggling around with
IsEnabledand setting the focus to the input field, i think there might be some problem in the order the triggers, styles and function calls in the Class of my Control. Anyway, i’ve solved it in the following way:I removed these three setters
to
And enabled the buttons all the time. After having applied the style triggers to the
TagProperty, everything looks the same and works perfectly. There might be a problem else to focus a disabled element (depends on the order the setters, styles and code within the Class itself are invoked).