c# - Connect working Binding to a List<string> -
i binding property of object capturenameslist
. variable need bind list<string>
.
it doesn't work when bind straight list<string>
. created wrapper class strings, stringwrapper
, , works when use list<stringwrapper>
_test4 backing variable. however, need somehow link _test1. attempt shown commented out, doesn't seem work.
how bind list<string>
?
xaml:
<itemscontrol itemssource="{binding capturenameslist}"> <itemscontrol.itemtemplate> <datatemplate> <textbox text="{binding path=value, mode=twoway, updatesourcetrigger=propertychanged}"/> </datatemplate> </itemscontrol.itemtemplate> </itemscontrol>
c#:
public list<stringwrapper> capturenameslist { { return _test4; } set { _test4 = value; } //get { return stringwrapper.castlist(_test1); } //set { _test1 = stringwrapper.castbacklist(value); } } private list<stringwrapper> _test4 = stringwrapper.castlist(new list<string> { "one", "two" }); private list<string> _test1 = new list<string> { "one", "two" };
the wrapper class strings:
public class stringwrapper { public string value { get; set; } static public explicit operator stringwrapper(string value) { return new stringwrapper() {value=value}; } static public explicit operator string(stringwrapper value) { return value.value; } public static list<stringwrapper> castlist(list<string> strings) { list<stringwrapper> wrappers = new list<stringwrapper>(); strings.foreach(item => wrappers.add((stringwrapper)item)); return wrappers; } public static list<string> castbacklist(list<stringwrapper> wrappers) { list<string> strings = new list<string>(); strings.foreach(item => strings.add((string)item)); return strings; } }
binding list<string>
should same, except in datatemplate
don't have specify property binding:
<itemscontrol itemssource="{binding capturenameslist}"> <itemscontrol.itemtemplate> <datatemplate> <textbox text="{binding mode=twoway, updatesourcetrigger=propertychanged}"/> </datatemplate> </itemscontrol.itemtemplate> </itemscontrol>
Comments
Post a Comment