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

ComboBox with Dictionary and ClearSelection

1 Answer 875 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Dario Concilio
Top achievements
Rank 2
Dario Concilio asked on 27 Sep 2016, 07:39 AM

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>

1 Answer, 1 is accepted

Sort by
0
Nasko
Telerik team
answered on 29 Sep 2016, 07:24 AM
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.
Tags
ComboBox
Asked by
Dario Concilio
Top achievements
Rank 2
Answers by
Nasko
Telerik team
Share this question
or