禁用/阻止在wpf中禁用组合框项目的选择 [英] Disallow/Block selection of disabled combobox item in wpf

查看:146
本文介绍了禁用/阻止在wpf中禁用组合框项目的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个应用程序,其中我想禁用 ComboBox 中的几个项目,并且还要禁止/阻止选择禁用的项目。请注意ComboBox在主窗口中有另一个ComboBox作为ComboBox项目init(在运行时由数据模板选择器决定)。

I'm writing an application wherein I would like to disable few items in the ComboBox and also want to disallow/block selection of disabled items. Please note ComboBox in main window has another ComboBox as ComboBox Item init (that is decided at run time by data template selector).

使用以下代码,我可以在ComboBox中禁用ComboBox,但不会阻止用户选择禁用的ComboBox项目。

With below code I'm able to disable a ComboBox within ComboBox but it would not stop user from selecting that disabled ComboBox item. Any help in disallow/block selection of disabled items would be helpful.

以下是代码段

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

数据模板选择器

public class QualityComboBoxTemplateSelector : DataTemplateSelector
{
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        var element = container as FrameworkElement;

        var dataTemplate = element.FindResource(((item is string) && item.Equals("Ferrari")) ?
                                                       "DataTemplateTopLevelCombobox2" : "DataTemplateTopLevelCombobox1") as DataTemplate;

        return dataTemplate;
    }
}

上述ComboBox的数据模板:

<DataTemplate x:Key="DataTemplateTopLevelCombobox1">
    <Border BorderBrush="Black" BorderThickness="1" >
        <TextBlock HorizontalAlignment="Left" 
                   TextWrapping="Wrap" Text="{Binding}"     
                   VerticalAlignment="Top"/>
    </Border>
</DataTemplate>

<DataTemplate x:Key="DataTemplateTopLevelCombobox2">
    <Border Width="100">
        <ComboBox Text="Custom" Height="21.96"
        ItemsSource="{Binding DataContext.Models, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
        IsEnabled="{Binding DataContext.EnableCombo, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
    </Border>
</DataTemplate>


推荐答案

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

这篇关于禁用/阻止在wpf中禁用组合框项目的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆