I define this simple class
public class SimpleClass
{
public string Val1 {get;set;};
public string Val2 {get;set;}
public string Res
{
get
{
string.Format("{0}_{1}", Val1, Val2 );
}
}
public SimpleClass(string v1, string v2)
{
Val1 = v1;
Val2 = v2;
}
public SimpleClass(string v1, int i)
{
if(i == 0)
{
Val1 = v1;
val2 = "";
}
if(i == 0)
{
Val2 = v1;
val1 = "";
}
}
}
Now, i define in the code this
List< SimpleClass > myList = new List<SimpleClass>();
myList.Add(new SimpleClass("a1", "b1");
myList.Add(new SimpleClass("a2", "b2");
myList.Add(new SimpleClass("a3", "b3");
myList.Add(new SimpleClass("a4", "b4");
And i define in the xaml 2 ListBox –
First that show all the a1…a4 items
Second that show all the b1…b4 items
Each item in the ListBox is CheckBox – and the content is the string of the item.
Now i want to define filter that will show in some other list only the SimpleClass.Res that was checked in the listBox.
==> that mean that if in the listBox that items that was checked are b1 and a3 that the only text in the third listbox will contain
a1_b1
a3_b3
How can i do it ?
I trying to use the CollectionViewSource but i can’t define multi filter in this cases.
In UI you will have to have the checkbox bound to something so that you are aware of the status of the checked items from both ListBoxes.
Hence we bind it
TwoWayto the ancestorListBoxItem‘sIsSelectedproperty. This way when checkboxes are checked they automatically register the checked item underListBox.SelectedItemsproperty.Based on this we perform multi binding
ListBox3.ItemsSourcetoListBox1.SelectedItemsandListBox2.SelectedItems. The multi value converter calledMergeSelectedItemsHelpersimply performs a union of the two selected items lists.I have used
ArrayofTextBlockinstead ofListofSimpleClassto bind to all those ListBoxes.XAML:
Code Behind:
Now as
ListBox.SelectedItemsproperty is simply a non dependency property and also non-observable so the bindings will not automatically refresh. In code behind we will hage to refresh the binding when seletion is changed on theListBox1andListBox2.As said earlier, the multi value converter simply joins the selected items together and gets called when multi-binding is refreshed…
Hope this helps.