简体   繁体   English

在组合框中禁用下拉菜单的选择

[英]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. 我有一个comboBox,其样式设置为具有文本块和复选框行,以允许用户检查不同事物的列表。 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 ) 我有同样的问题,我通过结合使用ToggleButtonPopup (而不是使用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 您可以尝试在selectionchanged事件中将组合框的selectedindex设置为-1

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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