Dictionary binding to ListBox Control

2 posts, 0 answers
  1. Dharmavaram
    Dharmavaram avatar
    73 posts
    Member since:
    Jun 2013

    Posted 15 Sep Link to this post

    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.

  2. Polya
    Admin
    Polya avatar
    200 posts

    Posted 19 Sep Link to this post

    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.
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top