Microsoft Developer Network > Forenhomepage > Windows Presentation Foundation (WPF) > Focus Combobox when User Control is focused.
Stellen Sie eine FrageStellen Sie eine Frage
 

BeantwortetFocus Combobox when User Control is focused.

  • Mittwoch, 4. November 2009 18:34RonakPPatel TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    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.

Antworten

  • Montag, 9. November 2009 06:57Linda LiuMSFT, ModeratorTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet

    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.

Alle Antworten

  • Donnerstag, 5. November 2009 09:00Geert van Horrik TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     

    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!
  • Donnerstag, 5. November 2009 16:15RonakPPatel TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    I forgot to take out the comment before posting it. I have also tried removing key but still no luck.
  • Freitag, 6. November 2009 17:37RonakPPatel TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    Any one?
  • Montag, 9. November 2009 06:57Linda LiuMSFT, ModeratorTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet

    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.