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

Dictionary binding to ListBox Control

1 Answer 1329 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Dharmavaram
Top achievements
Rank 1
Dharmavaram asked on 15 Sep 2016, 10:22 AM

Hi,

    I am trying to bind dictionary object to ListBoxControl as shown below

         <telerik:RadListBox ItemsSource="{Binding Path=BooksAvailable}" Height="270">
            <telerik:RadListBox.ItemTemplate>
                <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <CheckBox IsChecked="{Binding Path=Value}"
                       Width="20" />
                    <TextBlock Text="{Binding Path=Key}"
                       Width="100" />
                </StackPanel>
                </DataTemplate>
            </telerik:RadListBox.ItemTemplate>
        </telerik:RadListBox>

      I want to do two way binding of the values so that if user unchecked I have to update internal Boolean value property.But it is giving exception two way binding can't be possible with dictionary value.

Please help to resolve this.

 

Regards,

Nagasree.

1 Answer, 1 is accepted

Sort by
0
Polya
Telerik team
answered on 19 Sep 2016, 02:05 PM
Hello Nagasree,

The issue is caused by the two-way binding with the read-only property Value - which is not supported:
"System.Windows.Markup.XamlParseException: A TwoWay or OneWayToSource binding cannot work on the read-only property 'Value' of type 'System.Collections.Generic.KeyValuePair`2[System.String,System.Boolean]'":

Because the two-way binding is not supported in the Microsoft's implementation of the KeyValuePair you can either use one-way binding with property Value or use an ObservableCollection<Custom_Class> instead of Dictionary.

I've prepared a sample project demonstrating the second approach. Please find it attached.

Hope this helps.

Regards,
Polya
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
ListBox
Asked by
Dharmavaram
Top achievements
Rank 1
Answers by
Polya
Telerik team
Share this question
or