【问题标题】:Disable selection of drop down items in ComboBox禁用在 ComboBox 中选择下拉项目
【发布时间】:2019-05-06 10:30:38
【问题描述】:

我有一个组合框,它的样式设置为包含多行文本块和复选框,以允许用户检查不同事物的列表。我正在尝试做的是禁用组合框中的一行选择。

我的查看代码:

<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>

这是我的下拉菜单:

这是我不想要的图片:

我想禁用用户选择特定行的功能,但保留他们选择下拉列表中各种复选框的功能。有没有办法设置这个样式?

【问题讨论】:

标签: c# wpf combobox styles


【解决方案1】:

我遇到了同样的问题,我通过使用 ToggleButtonPopup 解决了它(而不是使用 ComboBox

注意:未经测试

<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>

【讨论】:

  • 这太完美了!谢谢你。我只是改变了一些样式,让它看起来更好一点。
【解决方案2】:

您可以尝试在 selectionchanged 事件中将您的组合框的 selectedindex 设置为 -1

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

【讨论】:

    猜你喜欢
    • 2017-06-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 2012-10-20
    • 1970-01-01
    • 2023-04-09
    • 2011-12-24
    • 1970-01-01
    相关资源
    最近更新 更多