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

Binding Listbox to ObservableDictionary

3 Answers 765 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Amad
Top achievements
Rank 1
Amad asked on 11 Sep 2020, 06:59 AM
Hello,

I am working on an MVVM application.I have in view a ListBox which is bound to an ObservableDictionary in VM as follows:

C# VM:

private ObservableDictionary<string, bool> mCategories;
public ObservableDictionary<string, bool> Categories
{
get { return mCategories; }
set { mCategories = value; FirePropertyChanged("Categories"); }
}

Categories = new ObservableDictionary<string, bool>()
{
{ "Allgemein", true},
{ "Dampf", false},
{ "Fernwärme", false},
{ "Prozessabhängig", false},
{ "Stromverbrauch", false}
};



XAML VIEW:

<telerik:RadListBox ItemsSource="{Binding Categories}">
    <telerik:RadListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <CheckBox IsChecked="{Binding Value, Mode=TwoWay}"/>
                <TextBlock Text="{Binding Key, Mode=OneWay}"/>
            </StackPanel>
        </DataTemplate>
    </telerik:RadListBox.ItemTemplate>
</telerik:RadListBox>



I have the following problem: Binding seems not to work in the direction View to VM, that when I check a Checkbox the Value of the corresponding dictionary element will not be set to true, while it works in the other direction (VM to View) that when I set the Value of a dictionary elemnt in C# code (VM) the CheckBx is checked in View.

Any suggestion?

Thanks

3 Answers, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 15 Sep 2020, 10:20 AM

Hello Amad,

Would you find it possible to share your implementation of the ObservableDictionary class as the ones I find on the internet do not allow for a TwoWay binding to be set for the Value property of the items?

What I can suggest in order to pinpoint the issue is to isolate the binding outside the context of the RadListBox control. You can define a single CheckBox instance, bind it to the Value of the first item in the Categories collection and check whether the issue still occurs. If that is the case, you will need to modify the ObservableDictionary class to support this scenario.

Please let me know how this goes.

Regards,
Dilyan Traykov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Amad
Top achievements
Rank 1
answered on 15 Sep 2020, 11:28 AM

Hello Dilyan,

I followed the approach that you suggested. I used single checkboxes and that solved the problem.

Thanks for the reply.

0
Dilyan Traykov
Telerik team
answered on 15 Sep 2020, 11:54 AM

Hello Amad,

I'm glad to hear that the issue is resolved. Please let me know if I can further assist you with anything else.

Regards,
Dilyan Traykov
Progress Telerik

Five days of Blazor, Angular, React, and Xamarin experts live-coding on twitch.tv/CodeItLive , special prizes and more, for FREE?! Register now for DevReach 2.0(20).

Tags
ListBox
Asked by
Amad
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
Amad
Top achievements
Rank 1
Share this question
or