c# - What is the most effective way to 'align' two separate lists by ordinal in a single ItemsControl? -
over-simplifying our model purposes of example, let's have 2 lists of data, lista , listb, both of of type list<string>
. data perspective, not related. lista , listb can added to, removed from, or otherwise updated independently.
what we're trying display them both @ same time in same list, aligned ordinal position.
our first approach create new listmapping object follows:
public class listmapping { public int index{ get; set; } public string stringa{ get; set; } public string stringb{ get; set; } }
then create list<listmapping>
relating strings @ ordinal position 'x' of lista , listb , we'd initialize this:
var mappedlist = new list<listmapping>(); var maxitems = math.max(lista.count, listb.count); for(int index = 0; index < maxitems; index++) { var lm = new listmapping(){ index = index, stringa = (index < lista.count) ? lista[index] : null; stringb = (index < listb.count) ? listb[index] : null; } mappedlist.add(lm); }
the problem approach had manually manage new list of listmap objects. if item deleted listb, need manually shift listmapping.stringb properties 1 position 'realign' new listmapping.stringa. same thing insert, etc.
our next approach not store string values in listmapping, index, , make getters return value directly underlying lists, this...
public class listmapping { public int index{ get; set; } public string stringa{ get{ (index < lista.count) ? lista[index] : null; } } public string stringb{ get{ (index < listb.count) ? listb[index] : null; } } }
and we'd initialize list<listmapping>
object this...
var mappedlist = new list<listmapping>(); var maxitems = math.max(lista.count, listb.count); for(int index = 0; index < maxitems; index++) { var lm = new listmapping(){ index = index } mappedlist.add(lm); }
using design, we'd need trigger property changed notifications stringa , stringb properties of listmapping index have been affected operation on either lista or listb. cleaner , no held references source objects, had have reference list objects themselves. plus, still need manually manage overall list of listmapping items ensure there's @ least 'maxitems' items @ times. better, not ideal.
i can't wonder if there's way construct itemscontrol have 2 itemssource properties clever layout panel , itemcontainergenerator, seems i'd doing in ui i'm doing in data.
so, thoughts on way solve issue?
Comments
Post a Comment