C# wpf binding to a binding -


i'm writing questionnnaire library c# wpf. in have usercontrol called multiplechoiceoption. has dependencyproperty optiontype.

if optiontype "combobox" insert combobox.

i want able bind itemssource of combobox dependencyproperty of multiplechoiceoption created this:

public static readonly dependencyproperty multiplechoicecomboboxitemsproperty =          dependencyproperty.register("comboboxitems", typeof(list<string>), typeof(multiplechoiceoption));  ...  public list<string> optiontext     {         { return getvalue(multiplechoiceoptiontextproperty) list<string>; }         set { setvalue(multiplechoiceoptiontextproperty, value); }     } 

if optiontype "combobox" add combobox , set binding this:

 case "combobox":                 var combobox = new combobox                 {                     horizontalalignment = horizontalalignment.right                 };                  var b = new binding()                 {                     path = new propertypath(multiplechoicecomboboxitemsproperty),                     source = comboboxitems                 };                  combobox.setbinding(combobox.itemssourceproperty, b);                  combobox.selectionchanged += comboboxchanged;                 stackpanel.children.add(textblock);                 stackpanel.children.add(combobox);                 container.children.add(stackpanel);                 break; 

in demo app try set binding list<string>:

<wpfquestionnaire:multiplechoicequestion                     questionnumber="2.1"                     questiontext="what friuts of following?">                      <wpfquestionnaire:multiplechoiceoption                         optiontype="combobox"                         comboboxitems="{binding relativesource={                                             relativesource                                              mode=findancestor,                                             ancestortype={x:type window}},                                         path=questiontwooneoptions,                                         mode=twoway}"/>  </wpfquestionnaire:multiplechoicequestion> 

code behind:

  public partial class mainwindow : inotifypropertychanged {     public mainwindow()     {         initializecomponent();         loaded += onloaded;     }      private void onloaded(object sender, routedeventargs e)     {         myquestionnaire.questionnairesubmitted += onsubmitted;         questiontwooneoptions = new list<string>             {                 "apple",                 "orange",                 "pear"             };     }      private list<string> _questiontowoneoptions;      public event propertychangedeventhandler propertychanged;       protected virtual void onpropertychanged(string property)     {         propertychanged?.invoke(this, new propertychangedeventargs(property));     }      private void onsubmitted(object sender, questionnairesubmittedeventargs e)     {         foreach (var in e.allanswers)         {                debug.writeline(a.answer);         }     }      public list<string> questiontwooneoptions     {         { return _questiontowoneoptions; }         set         {             _questiontowoneoptions = value;             onpropertychanged(nameof(questiontwooneoptions));         }     }  } 

this results in following error:

system.windows.data error: 17 : cannot 'comboboxitems' value (type 'list`1') '' (type 'list`1'). bindingexpression:path=(0); dataitem='list`1' (hashcode=23674331); target element 'combobox' (name=''); target property 'itemssource' (type 'ienumerable') invalidcastexception:'system.invalidcastexception: unable cast object of type 'system.collections.generic.list`1[system.string]' type 'system.windows.dependencyobject'. 

i don't why it's trying cast list dependencyobject. guess i'm bit confused types use go binding between list<t> --> dependencyproperty --> itemssource.

the binding b create goes property multiplechoicecomboboxitemsproperty within comboboxitems list<string> , not have desired property. trying resolve dependencyproperty, source has cast dependencyobject leads error.

using source = this should solve problem.


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -