This is a migrated thread and some comments may be shown as answers.

Disable RadComboBox items using Converter

2 Answers 464 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Pankhuri
Top achievements
Rank 1
Pankhuri asked on 14 Sep 2017, 07:15 AM

Hi,

I am trying to disable 2-3 options from my combo box using converter. I do not want to use IsEnabled property as my data is dynamically populated. Therefore, I have created a converter for the same. 

However, when I am using <Style.Triggers> my combo box is coming blank.

PFB the xaml code used.

 

 <ScrollViewer.Resources>
        <converters:ComboboxDisableConverter x:Key="itemDisableconverter"/>
        <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>

 

 <telerik:RadComboBox Grid.Row="0" Grid.Column="1" Name="MeterTypes"
                                     VerticalContentAlignment="Center"
                                     IsReadOnly="False"
                                     DisplayMemberPath="Name"
                                     ClearSelectionButtonVisibility="Visible"
                                    ItemsSource="{Binding MeterTypes}">
                        <!--SelectedItem ="{Binding MeterTypes, Converter={StaticResource itemDisableconverter}, ConverterParameter={StaticResource itemDisableconverter}, Mode=TwoWay}">-->

                        <telerik:RadComboBox.ItemContainerStyle>
                            <Style TargetType="{x:Type telerik:RadComboBoxItem}">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding Path=Text,
                                        RelativeSource={RelativeSource Self}, Converter={StaticResource itemDisableconverter}, ConverterParameter={StaticResource itemDisableconverter}}" Value="true">
                                        <Setter Property="IsEnabled" Value="False"/>
                                    </DataTrigger>
                                </Style.Triggers>
                                <Setter Property="IsEnabled" Value="True"/>
                            </Style>
                        </telerik:RadComboBox.ItemContainerStyle>

 

Instead when I am using the commented SelectedItem, my ComboBox is populated but my options are not greyed out.

Can you please help me here.

Thanks.

 

2 Answers, 1 is accepted

Sort by
0
Accepted
Dilyan Traykov
Telerik team
answered on 18 Sep 2017, 03:17 PM
Hello Pankhuri,

A possible reason why your RadComboBox is not populated when using the Style.Triggers approach is that you have not based the style for the RadComboBoxItem elements on the RadComboBoxItemStyle static resource.

<telerik:RadComboBox Height="35" ItemsSource="{Binding Players}" DisplayMemberPath="Name">
    <telerik:RadComboBox.ItemContainerStyle>
        <Style TargetType="{x:Type telerik:RadComboBoxItem}" BasedOn="{StaticResource RadComboBoxItemStyle}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Name, Converter={StaticResource itemDisableconverter}}" Value="False">
                    <Setter Property="IsEnabled" Value="False"/>
                </DataTrigger>
            </Style.Triggers>
            <Setter Property="IsEnabled" Value="True"/>
        </Style>
    </telerik:RadComboBox.ItemContainerStyle>
</telerik:RadComboBox>

Please also note that the RadComboBoxItem does not have a Text property and thus, the converter will never be reached when using the RelativeSource binding from your last reply. Instead, you can use either the DataContext (the bound data item) or the Content property of the items to perform your logic.

For your convenience, I'm attaching a working sample project based on the code you provided.

I hope you find all of this helpful. Do let me know if you require any further assistance on the matter.

Regards,
Dilyan Traykov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which you to write beautiful native mobile apps using a single shared C# codebase.
0
Pankhuri
Top achievements
Rank 1
answered on 19 Sep 2017, 09:38 AM

Hi Dilyan,

Thank you so much for your help.

It worked.

 

 

Tags
ComboBox
Asked by
Pankhuri
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
Pankhuri
Top achievements
Rank 1
Share this question
or