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
Other answers
You could try: ItemsSource="{Binding ElementName=Settings, Path=EffectWindowViewModel.Effects}"
SuperOli
Related Q & A:
- How To Send a Complex Object to a Rest Web Service?Best solution by codeproject.com
- How to build a spreadsheet-like control in WPF?Best solution by Stack Overflow
- How to highlight a row in a listview?Best solution by Stack Overflow
- How to convert a char array into an int in C++?Best solution by Stack Overflow
- How to refresh a listview within a Fragment?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.