user2851726
user2851726

Reputation: 53

How to make text clickable in wpf combobox?

Currently my combobox is working properly although i would like that when users press the Text field of my combobox the dropdownmenu shows.

I could do this in code for example with an event such as MouseLeftButtonUp but this works poorly especially when pressing the mouse left button several times in a row on the title.

                <ComboBox Name="comboBox1" IsTextSearchEnabled="False"  HorizontalContentAlignment="Stretch" Grid.Column="1" IsEditable="True" DropDownClosed="OnDropDownClosed" Text="The title for my combobox" Margin="30,0,0,0" Grid.ColumnSpan="2"  IsReadOnly="True">
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <CheckBox Name="CheckBox1" IsChecked="{Binding IsChecked, Mode=TwoWay}" Visibility="{Binding IsVisible, Mode=TwoWay}">
                                <CheckBox.Content>
                                    <TextBlock Text="{Binding name}" Margin="5" />
                                </CheckBox.Content>
                            </CheckBox>
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>

Upvotes: 0

Views: 489

Answers (1)

mm8
mm8

Reputation: 169240

You could handle the GotFocus or GotKeyboardFocus event like this:

private void comboBox1_GotFocus(object sender, RoutedEventArgs e)
{
    if (!comboBox1.IsDropDownOpen)
        comboBox1.IsDropDownOpen = true;
}

This works but when pressing several times on the combobox the box wont open again propably because the focus has not changed

Handle the Loaded event for the ComboBox and hook up an event handler for the PreviewMouseLeftButtonDown of the TextBox then:

private void comboBox1_Loaded(object sender, RoutedEventArgs e)
{
    ComboBox cmb = sender as ComboBox;
    TextBox textBox = cmb.Template.FindName("PART_EditableTextBox", cmb) as TextBox;
    if (textBox != null)
    {
        textBox.PreviewMouseLeftButtonDown += (ss, ee) =>
        {
            if (!comboBox1.IsDropDownOpen)
                comboBox1.IsDropDownOpen = true;
        };
    }
}

Upvotes: 1

Related Questions