【WPF】ComboBoxItem的禁用

70 篇文章 4 订阅

需求:下拉列表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);
}

最终效果如下图:

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值