ComboBox with Dictionary and ClearSelection

2 posts, 0 answers
  1. Dario Concilio
    Dario Concilio avatar
    153 posts
    Member since:
    Apr 2016

    Posted 27 Sep 2016 Link to this post

    Hi,

    I'm creating a ComboBox that contains a Dictionary<byte,string> (for ex.) and ClearSelectionbuttonVisibility = Visible.

    It's all ok, but if I click on "Clear Selection", it gives me ad error (that I attached).

    I had to declare my property SelectedItem as KeyValuePair<byte, string>.

    Now, I think that the reason's error is that KeyValuePair<byte,string> is not nullable, then?

    How can I do use Dictionary in this context?

    <!--My ComboBox for Dictionary-->
    <telerik:RadComboBox ItemsSource="{Binding MyItems}"
                         SelectedItem="{Binding SelectedItem,Mode=TwoWay}"
                         DisplayMemberPath="Value"
                         SelectedValuePath="Key"
                         Style="{StaticResource FieldLongComboBoxStyle}"/>
                          
     
    <!--Style-->
    <Style x:Key="FieldBaseComboBoxStyle" TargetType="telerik:RadComboBox" BasedOn="{StaticResource RadComboBoxStyle}">
        <Setter Property="Margin" Value="5"/>
        <Setter Property="ClearSelectionButtonVisibility" Value="Visible"/>
        <Setter Property="ClearSelectionButtonContent" Value="Clear selection"/>
        <Setter Property="TextSearchMode" Value="StartsWith"/>
        <Setter Property="EmptyText" Value="Valore non selezionato"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding CantUpdateModel}" Value="True">
                <Setter Property="IsReadOnly" Value="True"/>
                <Setter Property="IsEditable" Value="False"/>
                <Setter Property="IsHitTestVisible" Value="False"/>
                <Setter Property="FontWeight" Value="Bold"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding CantUpdateModel}" Value="False">
                <Setter Property="IsReadOnly" Value="False"/>
                <Setter Property="IsEditable" Value="True"/>
                <Setter Property="IsHitTestVisible" Value="True"/>
                <Setter Property="FontWeight" Value="Normal"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

  2. Nasko
    Admin
    Nasko avatar
    652 posts

    Posted 29 Sep 2016 Link to this post

    Hi Dario,

    The observed by you behavior is an expected one and could be observed with the native WPF ComboBox as well - if you simply clear the current SelectedItem the validation will be observed. When the selection is cleared the SelectedIndex is set to -1 and the SelectedItem is cleared - as you are binding to KeyValuePair the value cannot be converted as expected and the validation is visualized.

    What we could suggest you is to use a converter for changing the cleared null value to a KeyValuePair. Check the attached sample that demonstrates that approach.

    Hope this helps.

    Regards,
    Nasko
    Telerik by Progress
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Back to Top