且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

禁用/阻止在wpf中禁用组合框项目的选择

更新时间:2022-01-14 07:41:34

> IsEnabled 属性 ComboBoxItem false

You can achieve this by setting IsEnabled property of a ComboBoxItem to false;

因此ComboBox的 ItemSource (即 Cars 在你的case)可以是一个对象有一些属性(例如 IsSelectable )指定它是应该启用或禁用,然后使用它与样式,使项目不可选择。

So each item in ComboBox's ItemSource (i.e. Cars in your case) can be an object having some property (say IsSelectable) specifying whether it should be enabled or disabled and then use it with a style to make an item un-selectable. something like this -

<Style TargetType="ComboBoxItem"> 
   <Setter Property="IsEnabled" Value="{Binding IsSelectable}"/> 
</Style> 

更新:

<Grid>
    <ComboBox
        Width="120"
        Margin="87.2,44.8,0,0"
        HorizontalAlignment="Left"
        VerticalAlignment="Top"
        ItemTemplateSelector="{StaticResource QualityComboBoxTemplateSelector}"
        ItemsPanel="{DynamicResource ItemsPanelTemplateHorizontal}"
        ItemsSource="{Binding Cars}"
        SelectedItem="{Binding SelectedItm}">

        <ComboBox.ItemContainerStyle>
            <Style TargetType="ComboBoxItem">
                <Setter
                    Property="IsEnabled"
                    Value="{Binding IsSelectable}" />
            </Style>
        </ComboBox.ItemContainerStyle>

    </ComboBox>
</Grid>