How can I disable the selection event on this WPF ComboBox without disabling it?

Viewed 224

I have the following ComboBox XAML:

<ComboBox Width="350" ItemsSource="{Binding RunSets}" SelectedItem="{Binding SelectedRunSet, Mode=OneWay}" VerticalAlignment="Center" Height="20" Margin="5,1,0,0" Background="DimGray" Foreground="White" >
    <ComboBox.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel />
        </ItemsPanelTemplate>
    </ComboBox.ItemsPanel>
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <Grid Width="320">
                <CheckBox IsChecked="{Binding IsSelected}" Margin="2,2,10,0" Visibility="{Binding ExchangeMarketNamesVisibility}" PreviewMouseDown="RunsetComboBoxItem_PreviewMouseDown">
                    <CheckBox.Content>
                        <StackPanel PreviewMouseDown="RunsetComboBoxItem_PreviewMouseDown">
                            <StackPanel Orientation="Horizontal" PreviewMouseDown="RunsetComboBoxItem_PreviewMouseDown">
                                <TextBlock Text="{Binding DisplayName}"  PreviewMouseDown="RunsetComboBoxItem_PreviewMouseDown"/>
                            </StackPanel>
                            <StackPanel PreviewMouseDown="RunsetComboBoxItem_PreviewMouseDown">
                                <TextBlock Text="{Binding ExchangeMarketNames, StringFormat= {0}}" Visibility="{Binding ExchangeMarketNamesVisibility}" Margin="2,2,0,2" Foreground="LightGray"  PreviewMouseDown="RunsetComboBoxItem_PreviewMouseDown" />
                            </StackPanel>
                        </StackPanel>
                    </CheckBox.Content>
                </CheckBox>
                <TextBlock Text="Select Items..." Visibility="{Binding DefaultItemVisibility}"/>
            </Grid>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

The goal I'm trying to achieve is to have the ComboBox present a set of selectable ComboBoxItems. It should always display "Select Items" when closed:

enter image description here

And when opened I would like it to display the following:

enter image description here

When the highlighted area is clicked it should check the checkbox without closing the combobox. I have managed to get most of the behaviour I want by hacking the Visibility property to hide the checkbox in the "Select Items..." ComboBoxItem. Together with capturing the PreviewMouseDown event:

        private void RunsetComboBoxItem_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            var checkBox = sender as CheckBox;
            if(checkBox != null)
            {
                checkBox.IsChecked = !checkBox.IsChecked;

                e.Handled = true;
                
            }
        }

This is almost working perfectly, but there are two issues that I can't resolve:

  1. If I click on the border between the ComboBoxItems in the dropdown it will select that ComboBoxItem. I want it to never select any of them. I have managed to work around for clicks inside the highlighted area by capturing the preview mouse down event on the Grid in the and marking it handled. Unfortunately in the border this doesn't get triggered and the SelectedItemChanged event gets triggered on the ComboBox. This causes the selected item to change (as mentioned, I want the ComboBox to always display the "Select Items..." ComboBoxItem.

  2. The "Select Items..." is shown twice when the combo box is expaned. I would like it to show just once.

I have managed to get most of the behaviour I want by hacking the Visibility property to hide the checkbox in the "Select Items..." ComboBoxItem together with capturing the PreviewMouseDown event.

I have read through many stack overflow posts about this, and I have not found a way of doing this. I tried capturing the SelectionChange and marking it handled but it closed the combobox anyway, and there is no PreviewSelectionChange event. When an item is selected I'd like the combobox to stay open.

Is there any way I can achieve this? Please let me know if there's any more information I can add to clarify my problem. Thanks!

0 Answers
Related