Problem in databinding a dictionary in ListView combo-box column.
- by Ashish Ashu
I have a listview of which itemsource is set to my custom collection, let's say MyCollection. The code below is not full code , it's just a code snippets to explain the problem.
class Item : INotifyPropertyChanged
{
Options _options;
public Options OptionProp
{
get { return _options; }
set { _options = value; OnPropertyChanged ("OptionProp");}
}
string _Name;
public string NameProp
{
get { return _Name; }
set { _Name = value; OnPropertyChanged ("NameProp");}
}
}
class Options : Dictionary<string,string>
{
public Options()
{
this.Clear();
this.Add("One" , "1" );
this.Add("Two" , "2" );
this.Add("Three" , "3" );
}
}
MyCollection in my viewModel
class viewModel
{
ObservableCollection<Item> **MyCollection**;
KeyValuePair<sting,string> **SelectedOption**;
}
The listview Item Source is set to my MyCollection.
<ListView ItemSource = MyCollectoin>
I Listview contains two columns of which I have defined a datatemplats in the listview.
First column is a combo-box of which Itemsource is set to Options ( defined above )
Second column is a simple textblock to display Name.
Problem 1. I have defined a datatemplate for first column in which I have a combo box , I have set the
Itemsource =**MyCollection**
and
SelectedItem = SelectedOption
of the combo-box.
User can perform following operations in the listview:
Add ( Add the row in the listview )
Move Up ( Move row up in the listview )
Move Down ( Move down the item in the listview )
.Now when I add the row in the listview , the combo-box selected index is always comes to -1 (first column). However the combo box contains options One, Two and Three.
Also, I have initialized the SelectedOption to contain the first item, i:e One.
problem 2.
. Let suppose, I have added a single row in a listview and I have selected Option "one" in the combo box manually. Now when I perform Move Up or Move Down operations the selected index of a combo box is again set to -1. In the Move Up or Move Down operation , I am calling MoveUp and MoveDown methods of the Observable collection.
Probelm 3
How to serialize the entire collection in XML. Since I can't serialize the Dictionary and KeyValue Pair.
I have to restore the state of the listview.