How do I properly bind ObservableCollection to listview?

How to bind a ListView to an ObservableCollection<KeyValuePair<int,object>> inside a ViewModel in WPF?

  • Something like this: public class EffectViewModel { public string Name ... ObservableCollection<KeyValuePair<int,object>> settings public ObservableCollection<KeyValuePair<int,object>> Settings { get {return this.settings;} set { this.settings = value; this.RaisePropertyChanged ( "Settings" ); } } } Right now I am trying to bind it like this: EffectWindowViewModel.Effects is of type ObservableCollection<EffectViewModel>. <ListView Width="1000" Height="600" ItemsSource="{Binding EffectWindowViewModel.Effects}"> <ListView.View> <GridView> <GridViewColumn Width="Auto" DisplayMemberBinding="{Binding Key}" Header="Name" /> <GridViewColumn Width="Auto" DisplayMemberBinding="{Binding Value}" Header="Value" /> </GridView> </ListView.View> </ListView> But I don't know how to specify .Settings property. Any ideas?

  • Answer:

    your actual binding will not work cause EffectViewModel has no key and Value Property. i really dont know what your listview should display. if you want a list of EffectViewModels then the Itemssource is right. if you want further for each EffectViewModel to display the settings. then you need somekind of itemsscontrol with Itemssource={Binding Settings}. this itemsscontrol of course will need a itemsstemplate with your Key and Value. i have no VS here atm, but your GridViewColumn needs a kind of CellTemplate. and this template should consist of a itemscontrol. because you have 2collections! this code is probably not right but should take you in the right direction <ListView Width="1000" Height="600" ItemsSource="{Binding EffectWindowViewModel.Effects}"> <ListView.View> <GridView> <GridViewColumn DisplayMemberBinding="{Binding Settings}"> <GridViewColumn.CellTemplate> <DataTemplatex:Key="myCell4Settings"> <ListView ItemsSource="{Binding.}"> <ListView.View> <GridView> <GridViewColumn Width="Auto" DisplayMemberBinding="{Binding Key}" Header="Name" /> <GridViewColumn Width="Auto" DisplayMemberBinding="{Binding Value}" Header="Value" /> </GridView> </ListView.View> </GridViewColumn.CellTemplate> </GridViewColumn> </ListView> </DataTemplate> </GridView> </ListView.View> btw you could also use 2 lists independent. one parent Combobox or listbox (x:Name=parent) with itemssource=EffectWindowViewModel.Effects and a second ListView like you have, with the itemssource binding: ItemsSource="{Binding ElementName=parent, Path=SelectedItem.Settings}"

Joan Venge at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

You could try: ItemsSource="{Binding ElementName=Settings, Path=EffectWindowViewModel.Effects}"

SuperOli

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.