简体   繁体   中英

Disable selection of drop down items in ComboBox

I have a comboBox that is styled to have rows of text blocks and check boxes to allow user to check a list of different things. What I am attempting to do is disable selection of a row in the the combobox.

My View Code:

<ComboBox ItemsSource="{Binding HeaderList}" 
    IsSelected="False"
    HorizontalAlignment="Left"
    Height="10"
    x:Name="ComboBox">
    <ComboBox.ItemTemplate>
       <DataTemplate>
          <StackPanel Orientation="Horizontal">
             <CheckBox IsChecked="{Binding IsChecked}"/>
             <TextBlock Text="{Binding HeaderName}"/>
          </StackPanel>
       </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

Here is a picture of my Drop Down:

在此处输入图片说明

Here is a picture of what I don't want:

在此处输入图片说明

I want to disable the ability for the user to select a specific row, but keep the ability for them to select various check boxes in the drop down. Is there a way to style this?

I had the same issue, i solved it by using a ToggleButton in combination with a Popup (instead of using a ComboBox )

Note: Not tested

<ToggleButton x:Name="filterButton" />
<Popup x:Name="popup" 
    AllowsTransparency="True" 
    StaysOpen="False"
    PlacementTarget="{Binding ElementName=filterButton}"
    IsOpen="{Binding ElementName=filterButton,Path=IsChecked,Mode=TwoWay}">
    <ItemsControl ItemsSource="{Binding HeaderList}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <CheckBox IsChecked="{Binding IsChecked}"/>
                    <TextBlock Text="{Binding HeaderName}"/>
                </StackPanel>
            </DataTemplate>                                    
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Popup>

You can try to set the selectedindex of your combobox to -1 in the selectionchanged event

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM