# Enable\Disable RadComboBoxItems

The purpose of this tutorial is to show you how to enable and disable **RadComboBoxItems**.

## Enable\Disable RadComboBox with Static Data

In the simplest scenario you will have a **RadComboBox** populated with static data in XAML.

**Example 1: Populate with static data**

```XAML
<telerik:RadComboBox x:Name="radComboBox" Width="200">
    <telerik:RadComboBoxItem Content="Alapattah"/>
    <telerik:RadComboBoxItem Content="Brickell Avenue" />
    <telerik:RadComboBoxItem Content="Downtown Miami" />
</telerik:RadComboBox>
```

The **RadComboBox** and **RadComboBoxItem** classes expose a property named **IsEnabled**. Set this property to **False**, when you want to disable either the whole **RadComboBox** or specific **RadComboBoxItems**. The next code-snippet shows you how to do that.

**Example 2: Disabling specific RadComboBoxItems using the IsEnabled property**

```XAML
<telerik:RadComboBox Width="200">
    <telerik:RadComboBoxItem Content="Alapattah" IsEnabled="False"/>
    <telerik:RadComboBoxItem Content="Brickell Avenue" />
    <telerik:RadComboBoxItem Content="Downtown Miami" IsEnabled="False"/>
</telerik:RadComboBox>
```

> caption Result of example 2

![Telerik UI for WPF RadComboBox with disabled items](https://www.telerik.com/products/wpf/documentation/assets/29973fbb6364fe365075130fb0fa119f/radcombobox_howto_enabledisableitems_010.png)

## Enable\Disable Items Using ItemContainerStyle

Using **RadComboBox** with static data is the simplest scenario. However, in most of the cases you will have a **RadComboBox** [populated with a collection of business objects](https://www.telerik.com/products/wpf/documentation/controls/radcombobox/populating-with-data/binding-to-object). In this case you have no other options except for using either the **ItemContainerStyle** or the **ItemContainerStyleSelector**.

**Example 3: Create a collection of objects**

```C#
public class Item : ViewModelBase
{
    public int Id { get; set; }
    public string Text { get; set; }
}
```

**Example 4: Create the ViewModel**

```C#
public class MainViewModel : ViewModelBase
{
    private ObservableCollection<Item> items;

    public ObservableCollection<Item> Items
    {
        get
        {
            if (this.items == null)
            {
                this.items = new ObservableCollection<Item>();
                for (int i = 0; i < 20; i++)
                {
                    this.items.Add(new Item() { Id = i, Text = "Item " + i });
                }
            }
            return this.items;
        }
    }
}
```

**Example 5: Set the ItemContainerStyle of a RadComboBox**

```XAML
<Grid.DataContext>
    <local:MainViewModel/>
</Grid.DataContext>
<Grid.Resources>
    <Style x:Key="ItemContainerStyle" TargetType="telerik:RadComboBoxItem">
        <Setter Property="IsEnabled" Value="False"/>
    </Style>
</Grid.Resources>

<telerik:RadComboBox
        	Width="200"
ItemsSource="{Binding Items}"
DisplayMemberPath="Text"
ItemContainerStyle="{StaticResource ItemContainerStyle}"/>
```

Note that in this case all **RadComboBoxItems** will be disabled. However, you have the ability to determine which items to be disabled based on your custom logic. [Check out](https://www.telerik.com/products/wpf/documentation/controls/radcombobox/howto/enable-disable-radcombobox-items#enabledisable-items-using-itemcontainerstyleselector) the next section to see the solution.

> caption Result of example 5

![Telerik UI for WPF RadComboBox with all items disabled by an ItemContainerStyle](https://www.telerik.com/products/wpf/documentation/assets/ad16fc28374ab63b290f79202843b489/radcombobox_howto_enabledisableitems_020.png)

## Enable\Disable Items Using ItemContainerStyleSelector

The **StyleSelector** provides a way to apply styles based on custom logic.

The next example demonstrates you how to achieve this.

**Example 6: Create a custom logic class**

```C#
public class EnableDisableSelector : StyleSelector
{
    public Style EnableStyle { get; set; }
    public Style DisableStyle { get; set; }

    public override Style SelectStyle(object item, DependencyObject container)
    {
        if ((item as Item).Id % 2 == 1)
            return this.DisableStyle;
        else
            return this.EnableStyle;
    }
}
```

**Example 7: Apply styles based on custom logic**

```XAML
<Grid.Resources>
    <Style x:Key="EnableStyle" TargetType="telerik:RadComboBoxItem">
        <Setter Property="IsEnabled" Value="True"/>
    </Style>

    <Style x:Key="DisableStyle" TargetType="telerik:RadComboBoxItem">
        <Setter Property="IsEnabled" Value="False"/>
    </Style>

    <local:EnableDisableSelector x:Key="StyleSelector" EnableStyle="{StaticResource EnableStyle}"
    DisableStyle="{StaticResource DisableStyle}"/>
</Grid.Resources>

<telerik:RadComboBox
        	Width="200"
ItemsSource="{Binding Items}"
DisplayMemberPath="Text"
ItemContainerStyleSelector="{StaticResource StyleSelector}"/>
```

> caption The result is that every odd item is disabled

![Telerik UI for WPF RadComboBox with every odd item disabled by an ItemContainerStyleSelector](https://www.telerik.com/products/wpf/documentation/assets/1dfeb8eeed31f45967426f2e494964db/radcombobox_howto_enabledisableitems_030.png)

## Using Style Binding

Using **ItemContainerStyle** is a good solution, however, it is not the best. Imagine that your business object has a boolean property named **IsEnabled**. Even more flexible solution is to directly bind the **RadComboBoxItem**'s **IsEnabled** property to the **IsEnabled** property exposed by your domain object. You can do this through **style binding**.

**Example 8: Use of style binding**

```XAML
<Grid.Resources>
    <Style x:Key="ItemContainerStyle" TargetType="{x:Type telerik:RadComboBoxItem}">
        <Setter Property="IsEnabled" Value="{Binding IsEnabled}"/>
    </Style>
</Grid.Resources>

<telerik:RadComboBox
        	Width="200"
ItemsSource="{Binding Items}"
ItemTemplate="{StaticResource CustomItemTemplate}"
ItemContainerStyle="{StaticResource ItemContainerStyle}"/>
```

Two things should be mentioned here. First, note how the **IsEnabled** property is bound in the **Style**. Second, the declared **Style** is set as a **ItemContainerStyle** of the **RadComboBox**. The result is shown on the image below.

> caption Picture 4: result of example 5

![Telerik UI for WPF RadComboBox with disabled items styled through property binding](https://www.telerik.com/products/wpf/documentation/assets/227c49d454e06a1e1a7821638f3cf40b/radcombobox_howto_enabledisableitems_040_wpf.png)

## See Also

* [Missing Controls in the UI](https://www.telerik.com/products/wpf/documentation/common-information/troubleshooting/invisible-controls)
* [RadComboBox Virtualization](https://www.telerik.com/products/wpf/documentation/controls/radcombobox/howto/virtualization)
* [Setting up AutoComplete](https://www.telerik.com/products/wpf/documentation/controls/radcombobox/howto/setting-up-autocomplete)
