Disable RadComboBox items using Converter

3 posts, 1 answers
  1. Pankhuri
    Pankhuri avatar
    2 posts
    Member since:
    Sep 2017

    Posted 14 Sep Link to this post

    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. Answer
    Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    603 posts

    Posted 18 Sep Link to this post

    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 allow you to write beautiful native mobile apps using a single shared C# codebase.
  3. Pankhuri
    Pankhuri avatar
    2 posts
    Member since:
    Sep 2017

    Posted 19 Sep Link to this post

    Hi Dilyan,

    Thank you so much for your help.

    It worked.

     

     

Back to Top