Search code examples
.netxamlcheckboxuwpcombobox

ComboBox without selection item. Only popup checkboxes


I have a comboBox. When opened it shows checkboxes.

Problem: While checking/unchecking one becomes selected item for my comboBox. I don't want that.

  • I want to disable selectable item, because logic is done when checking/unchecking items in comboBox's popup.
  • And preferably default text "-- Select flags --".

enter image description here

enter image description here

enter image description here

I tried:

  • IsReadOnly property does not exist. Cannot be selected. Maybe its due to my ItemTemplate because its not - TextBox (I don't know).
  • IsEditable="False" does not work
  • IsHitTestVisible, IsEnabled="False" make it unopenable. I cannot get to check boxes
            <ComboBox Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Top" 
                Name="flagComboBox" IsTextSearchEnabled="True" Margin="0,10,0,0"
                IsEditable="False" Text="-- Select flags -- (does not show)"
                >
                <ComboBox.ItemTemplate>
                    <DataTemplate >
                        <CheckBox Name="checkFileException"
                            Checked="Check_Flag" Unchecked="Uncheck_Flag" 
                            Content="{Binding}" 
                            CommandParameter="{Binding}"
                            >
                        </CheckBox>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>


Solution

  • You can do this:

    In XAML, first, change

    Text="-- Select flags -- (does not show)"
    

    with

    PlaceholderText="-- Select flags --"
    

    Then, add an event when the user change selection in the ComboBox

    SelectionChanged="ComboBox_SelectionChanged"
    

    In your code, add:

    private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            flagComboBox.SelectedIndex = -1;
    
        }
    

    The user will see the PlaceholderText in the ComboBox

    EDIT: If you want to keep open the ComboBox until the user press anywhere else on the screen (so the user will continue to see the ComboBox even if clicks on the text) you could do a workaround:

    In your XAML file, add three events to the ComboBox:

    GotFocus="flagComboBox_GotFocus" LostFocus="flagComboBox_LostFocus" DropDownClosed="flagComboBox_DropDownClosed"
    

    In code, create a new bool that indicates if the user is using the ComboBox

    public sealed partial class MainPage : Page
    {
        bool isPointerOnComboBox = false; // <-- add the bool here
        public MainPage()
    

    Then, add these three voids

    private void flagComboBox_DropDownClosed(object sender, object e)
        {
            if (isPointerOnComboBox.Equals(true))
            {
                flagComboBox.IsDropDownOpen = true;
            }
        }
    
        private void flagComboBox_GotFocus(object sender, RoutedEventArgs e)
        {
            isPointerOnComboBox = true;
        }
    
        private void flagComboBox_LostFocus(object sender, RoutedEventArgs e)
        {
            isPointerOnComboBox = false;
        }
    

    If the ComboBox got focus (it was selected by the user), the ComboBox will reopen automatically. If the ComboBox hasn't got focus (the user clicked somewhere else), the ComboBox won't reopen.