zoukankan      html  css  js  c++  java
  • 【WPF】ComboBoxItem的禁用

    需求:下拉列表ComboBox中,要求部分Item不可用。效果是鼠标一上去后不获得焦点,且无法点击。

    前台XAML界面:

    <!-- 下拉列表:省份 -->
    <ComboBox Grid.Column="0" Grid.Row="0" x:Name="provinceComboxBox" Margin="20,10,0,200" Height="20"
              ItemsSource="{Binding ProvinceList}" FontSize="12" Style="{StaticResource myComboBox_Useable}">
        <ComboBox.ItemContainerStyle>
            <Style TargetType="ComboBoxItem">
                <Setter  Property="IsEnabled" Value="{Binding isEnabled}" />
            </Style>
        </ComboBox.ItemContainerStyle>
    </ComboBox>

    样式文件如下:使得可用于不可用的Item文字颜色不同,显示的文字是实体类中的”provinceName”属性。

    <!-- 样式:x:Key="myComboBox_Useable" 下拉列表中,可能包含不可选的Item! -->
    <Style x:Key="myComboBox_Useable" TargetType="{x:Type ComboBox}">
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
    
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=provinceName}">
                        <TextBlock.Style>
                            <Style TargetType="TextBlock">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding isEnabled}" Value="True">
                                        <DataTrigger.Setters>
                                            <Setter Property="Foreground" Value="blue"/>
                                            <!--<Setter Property="IsEnabled" Value="{Binding isEnabled}" />--><!-- 经测试,在样式中写无效,改到在前台写 -->
                                        </DataTrigger.Setters>
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding isEnabled}" Value="False">
                                        <DataTrigger.Setters>
                                            <Setter Property="Foreground" Value="Pink"/>
                                            <!--<Setter Property="IsEnabled" Value="{Binding isEnabled}" />--><!-- 经测试,在样式中写无效,改到在前台写 -->
                                        </DataTrigger.Setters>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </TextBlock.Style>
                    </TextBlock>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    
    </Style>

    ViewModel中声明前台控件ItemsSource绑定到的列表

    private ObservableCollection<Provinces> provinceList;   // 所有省份的信息
    public ObservableCollection<Provinces> ProvinceList
    {
        get { return provinceList; }
        set { SetProperty(ref provinceList, value); }
    }

    注意,前台ComboBoxItem的”IsEnabled”属性绑定到的是Provinces实体类中的”IsEnabled”属性。

    public class Provinces
    {
        public int provinceId { get; set; }         // 省会ID
        public string provinceName { get; set; }    // 省会名称
        public bool isEnabled { get; set; }         // 该省份是否可用
    }

    控制层给ProvinceList列表填充数据即可。

    houseTypeViewModel.ProvinceList.Clear();
    houseTypeViewModel.ProvinceList = DataList; // 这是联网获取的数据!
    
    foreach (var item in houseTypeViewModel.ProvinceList)
    {
        // 模拟的数据
        if (item.provinceName.Equals("广西壮族自治区") || item.provinceName.Equals("广东省"))
        {
            item.isEnabled = true;
        }
    
        houseTypeViewModel.ProvinceName.Add(item.provinceName);
    }

    最终效果如下图:

    这里写图片描述

  • 相关阅读:
    CF1011B
    CF1011A
    AHOI2009 中国象棋
    jsp九大内置对象
    jsp七大动作指令
    navicat从下载到使用
    javaWeb应用部署结构浅析
    tomcat从下载到使用
    JavaWEB前端向服务器端发送对象
    初学者对Spring MVC的认识
  • 原文地址:https://www.cnblogs.com/guxin/p/wpf-combobox-how-to-disable-comboboxitem.html
Copyright © 2011-2022 走看看