Formular una preguntaFormular una pregunta
 

RespondidaFocus Combobox when User Control is focused.

  • miércoles, 04 de noviembre de 2009 18:34RonakPPatel Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    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}">
    <Style.Triggers>
    <Trigger Property="IsKeyboardFocused" Value="True">
    <Setter Property="Margin" Value="0,0,20,0"/>
    </Trigger>
    </Style.Triggers>
    </Style>
    This style works for textbox.

Respuestas

  • lunes, 09 de noviembre de 2009 6:57Linda LiuMSFT, ModeradorMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida

    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.

Todas las respuestas

  • jueves, 05 de noviembre de 2009 9:00Geert van Horrik Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     

    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!
  • jueves, 05 de noviembre de 2009 16:15RonakPPatel Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    I forgot to take out the comment before posting it. I have also tried removing key but still no luck.
  • viernes, 06 de noviembre de 2009 17:37RonakPPatel Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    Any one?
  • lunes, 09 de noviembre de 2009 6:57Linda LiuMSFT, ModeradorMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida

    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.