Focus Combobox when User Control is focused.
- I have a UserControl, which has one combobox and a button. I want to Shrink the Combobox when It has KeyBoard Focus. I got it working with textbox control.
Style is not working.
<UserControl x:Class="TextBoxStyle.WPFComboBox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" GotFocus="UserControl_GotFocus" Focusable="True" > <UserControl.Resources> <Style x:Key="{x:Type ComboBox}" TargetType="ComboBox" > <Style.Triggers> <Trigger Property="IsKeyboardFocused" Value="True" > <Setter Property="Margin" Value="0,0,25,0" /> </Trigger> </Style.Triggers> </Style> </UserControl.Resources> <StackPanel> <Grid > <Grid.RowDefinitions> <RowDefinition/> </Grid.RowDefinitions> <Button HorizontalAlignment="Right" Background="Transparent" Click="btnExpression_Click" Name="btnExpression" BorderBrush="Transparent" Focusable="False" > <Image Grid.Row="0" Source="/TextBoxStyle;component/Resources/folder_closed_16x16_XP.ico" SnapsToDevicePixels="True" Stretch="None" VerticalAlignment="Center" HorizontalAlignment="Center" Name="ebButton" /> </Button> <ComboBox Grid.Row="0" Name="cmbBox" IsEditable="True" Focusable="True" PreviewTextInput="cmbBox_PreviewTextInput" SelectionChanged="cmbBox_SelectionChanged" > <ComboBox.ToolTip> <ToolTip x:Name="toolTip" /> </ComboBox.ToolTip> </ComboBox> </Grid> </StackPanel> </UserControl>
<Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBoxBase}">This style works for textbox.
<Style.Triggers>
<Trigger Property="IsKeyboardFocused" Value="True">
<Setter Property="Margin" Value="0,0,20,0"/>
</Trigger>
</Style.Triggers>
</Style>- EditadoRonakPPatel jueves, 05 de noviembre de 2009 16:09
- EditadoRonakPPatel jueves, 05 de noviembre de 2009 16:14
- EditadoRonakPPatel jueves, 05 de noviembre de 2009 16:11
Respuestas
Hi RonakPPatel,
When a ComboBox is editable, the value of its IsKeyboardFocused property is FALSE even when the ComboBox has the keyboard focus. In fact, it's the TextBox within the visual tree of the ComboBox that really gets focused. The ComboBox's IskeyboardFocusWithin property does return true in this case. So use IsKeyboardFocusWithin property in the style for the ComboBox instead. For example:
<Style TargetType="ComboBox" >
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True" >
<Setter Property="Margin" Value="0,0,25,0" />
</Trigger>
</Style.Triggers>
</Style>
Sincerely,
Linda Liu
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Marcado como respuestaLinda LiuMSFT, Moderadormartes, 10 de noviembre de 2009 4:45
Todas las respuestas
Is there a reason why the style is commented? Remove the key from the style definition as well.
Geert van Horrik - CatenaLogic
Visit my blog: http://blog.catenalogic.com
Looking for a way to deploy your updates to all your clients? Try Updater!- I forgot to take out the comment before posting it. I have also tried removing key but still no luck.
- Any one?
Hi RonakPPatel,
When a ComboBox is editable, the value of its IsKeyboardFocused property is FALSE even when the ComboBox has the keyboard focus. In fact, it's the TextBox within the visual tree of the ComboBox that really gets focused. The ComboBox's IskeyboardFocusWithin property does return true in this case. So use IsKeyboardFocusWithin property in the style for the ComboBox instead. For example:
<Style TargetType="ComboBox" >
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True" >
<Setter Property="Margin" Value="0,0,25,0" />
</Trigger>
</Style.Triggers>
</Style>
Sincerely,
Linda Liu
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Marcado como respuestaLinda LiuMSFT, Moderadormartes, 10 de noviembre de 2009 4:45

