Click here to Skip to main content
15,887,485 members
Articles / Desktop Programming / WPF
Tip/Trick

Disabling ComboboxItem in ComboBox Control using Converter

Rate me:
Please Sign up or sign in to vote.
4.86/5 (3 votes)
24 Nov 2013CPOL 36.2K   430   8   1
WPF ComboBoxItem disabling

Introduction

Many would have come across a scenario where-in you need to disable some of the items in comboBox based on business needs or project requirements. This tip will help you to do the same.

Using the Code

Step 1: Create Combobox in XAML and bind ItemSource of ComboBox

XML
       <Window x:Class="ComboboxDisable.WPFComboboxWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ComboboxItemDisable" Height="165" Width="332"
        xmlns:local="clr-namespace:ComboboxDisable">
        
    <Grid Height="130" Width="316">
        <ComboBox Height="23" HorizontalAlignment="Left" 
        Margin="78,12,0,0" Name="comboBox1" 
        VerticalAlignment="Top" Width="120" 
                  ItemsSource="{Binding Items}" 
                  Background="Bisque" Foreground="Black"
                  SelectedIndex="0" >
        </ComboBox>
    </Grid>
</Window> 

Step 2: Create Class for Binding Itemsource with List<String> collections

C#
public class ExampleClass 
    {
        // Field for storing collection
        private List<String> items;
 
        // Property for Storing Collections
        public List<String> Items
        {
            get { return items; }
            set {items = value;}
            
        }
        /// <summary>
        /// Constructor
        /// </summary>
        public ExampleClass()
        {   
            items = new List<String>();
            items.Add("CollectionItem1");
            items.Add("CollectionItem2");
            items.Add("CollectionItem3");
            items.Add("CollectionItem4");
            items.Add("CollectionItem5");
        }
    }  

Step 3: Create Converter Class for Disabling Items in ComboBox

Below Converter will disable 2nd and 3rd Items from ComboBox [You can have your own custom logic for which condition the combobox item should be disabled].

C#
 namespace ComboboxDisable
{
    // Converter for disabling ComboboxItem
    class ComboboxDisableConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, 
        object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return value;
            // You can add your custom logic here to disable combobox item
            if (value == "CollectionItem2" || 
            	value == "CollectionItem3")
                return true;
 
            return false;
        }
 
        public object ConvertBack(object value, Type targetType, 
        object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}  

Step 4: Now modify your XAML file by specifying the ItemContainerStyle for ComboBox and including Converter

XML
//
 	    <!--Resource for Combobox to -->
            <ComboBox.Resources>
                <local:ComboboxDisableConverter x:Key="itemDisableconverter"/>
            </ComboBox.Resources>
//     
XML
<!--ItemContainer Style for disabling Combobox Item-->
            <ComboBox.ItemContainerStyle>
                <Style TargetType="ComboBoxItem">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=Content, 
                        RelativeSource={RelativeSource Self}, 
                            Converter={StaticResource itemDisableconverter}}" 
                            Value="true">
                            <Setter Property="IsEnabled" 
                            Value="False"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ComboBox.ItemContainerStyle> 

By including the above set of blocks in your XAML, your final XAML will look like the below one:

XML
<Window x:Class="ComboboxDisable.WPFComboboxWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ComboboxItemDisable" Height="165" Width="332"
        xmlns:local="clr-namespace:ComboboxDisable">
        
    <Grid Height="130" Width="316">
        <ComboBox Height="23" HorizontalAlignment="Left" 
        Margin="78,12,0,0" Name="comboBox1" 
        VerticalAlignment="Top" Width="120" 
                  ItemsSource="{Binding Items}" 
                  Background="Bisque" Foreground="Black"
                  SelectedIndex="0" >
  
            <!--Resource for Combobox to -->
            <ComboBox.Resources>
                <local:ComboboxDisableConverter x:Key="itemDisableconverter"/>
            </ComboBox.Resources>
 
            <!--ItemContainer Style for disabling Combobox Item-->
            <ComboBox.ItemContainerStyle>
                <Style TargetType="ComboBoxItem">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=Content, 
                        RelativeSource={RelativeSource Self}, 
                            Converter={StaticResource itemDisableconverter}}" 
                            Value="true">
                            <Setter Property="IsEnabled" 
                            Value="False"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ComboBox.ItemContainerStyle>
        </ComboBox>
    </Grid>
</Window> 

Execute the project and open the comboBox. You will see the 2nd and 3rd items are disabled and user can't select them.

You also can download the same from the attachment at the top of this tip.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionDisallow selection of disabled combox items in data template Pin
vishalmore2-Jan-14 8:35
vishalmore2-Jan-14 8:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.