Edit this page

Enable\Disable RadComboBoxItems

The purpose of this tutorial is to show you how to enable and disable RadComboBoxItems. The following sections are included:

Enable\Disable RadComboBox with Static Data

In the simplest scenario you will have a RadComboBox populated with static data in 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.

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

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. In this case you have no other options except for using either the ItemContainerStyle or the ItemContainerStyleSelector.

The next example shows you how to set the ItemContainerStyle of a RadComboBox.

<UserControl.Resources>
    <Style x:Key="ItemContainerStyle" TargetType="telerik:RadComboBoxItem">
        <Setter Property="IsEnabled" Value="False"/>
    </Style>
</UserControl.Resources>
 ...
<telerik:RadComboBox x:Name="radComboBox"
    ItemsSource="{Binding Source={StaticResource DataSource}, Path=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 the next section to see the solution.

Enable\Disable Items Using ItemContainerStyleSelector

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

For more information about the StyleSelectors take a look at the DataBinding topics in the Common help section.

The next example demonstrates you how to achieve this.

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

    <example:EnableDisableSelector x:Key="StyleSelector" EnableStyle="{StaticResource EnableStyle}"
        DisableStyle="{StaticResource DisableStyle}"/>
</UserControl.Resources>
 ...
<telerik:RadComboBox x:Name="radComboBox"
    ItemsSource="{Binding Source={StaticResource DataSource}, Path=Items}"
    DisplayMemberPath="Text"
    ItemContainerStyleSelector="{StaticResource StyleSelector}"/>

And the result is that every odd item is disabled.

Using MVVM Approach and ContainerBindingCollection

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 the ContainerBindingCollection class exposed by the Telerik UI for Silverlights' API.

<UserControl.Resources>
    <telerik:ContainerBindingCollection x:Name="BindingsCollection">
        <telerik:ContainerBinding PropertyName="IsEnabled" Binding="{Binding IsEnabled, Mode=TwoWay}" />
    </telerik:ContainerBindingCollection>

    <DataTemplate x:Key="CustomItemTemplate"
         telerik:ContainerBinding.ContainerBindings="{StaticResource BindingsCollection}">
        <TextBlock Text="{Binding Text}"/>
    </DataTemplate>
</UserControl.Resources>
 ...
<telerik:RadComboBox x:Name="radComboBox"
    ItemsSource="{Binding Source={StaticResource DataSource}, Path=Items}"
    ItemTemplate="{StaticResource CustomItemTemplate}"/>

In this case every even item is disabled and the logic about that is enclosed in the model.

Using Style Binding

<UserControl.Resources>
    <Style x:Key="ItemContainerStyle" TargetType="{x:Type telerik:RadComboBoxItem}">
        <Setter Property="IsEnabled" Value="{Binding IsEnabled}"/>
    </Style>
</UserControl.Resources>
 ...
<telerik:RadComboBox x:Name="radComboBox"
    ItemsSource="{Binding Source={StaticResource DataSource}, Path=Items}"
    ItemTemplate="{StaticResource CustomItemTemplate}"
    ItemContainerStyle="{StaticResource ItemContainerStyle}"/>

See Also