wpf - Why isn't my user control with a combobox binding correctly? -


i've got simple usercontrol i'm trying create contains list of states. trying expose selected state via "selectedstate" property. however, i'm having trouble trying binding fire once it's hooked in usercontrol / form.

the xaml user control looks this:

<usercontrol x:class="sample.desktop.usercontrols.statedropdown"          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"           xmlns:d="http://schemas.microsoft.com/expression/blend/2008"           xmlns:local="clr-namespace:sample.desktop.usercontrols"          mc:ignorable="d"           width="170" height="28"          d:designheight="28" d:designwidth="170"> <combobox x:name="cbostate"            itemssource="{binding statelist, relativesource={relativesource ancestortype=usercontrol}}"            selectedvalue="{binding selectedstate, mode=twoway, relativesource={relativesource ancestortype=usercontrol}}"            >     <combobox.itemtemplate>         <datatemplate>             <stackpanel orientation="horizontal">                 <label content="{binding abbreviation}"></label>                 <label> - </label>                 <label content="{binding name}"></label>             </stackpanel>         </datatemplate>     </combobox.itemtemplate> </combobox> 

in code-behind, have code:

    public static readonly dependencyproperty selectedstateproperty = dependencyproperty.register("selectedstate",                                                                                                    typeof(usstate),                                                                                                    typeof(statedropdown),                                                                                                    new uipropertymetadata(null,                                                                                                                           new propertychangedcallback(onselectedstatechanged),                                                                                                                           new coercevaluecallback(oncoerceselectedstate)));      private static object oncoerceselectedstate(dependencyobject o, object value)     {         statedropdown statedropdown = o statedropdown;         if (statedropdown != null)             return statedropdown.oncoerceselectedstate((usstate)value);         else             return value;     }      private static void onselectedstatechanged(dependencyobject o, dependencypropertychangedeventargs e)     {         statedropdown statedropdown = o statedropdown;         if (statedropdown != null)             statedropdown.onselectedstatechanged((usstate)e.oldvalue, (usstate)e.newvalue);     }      protected virtual usstate oncoerceselectedstate(usstate value)     {         // todo: keep proposed value within desired range.         return value;     }      protected virtual void onselectedstatechanged(usstate oldvalue, usstate newvalue)     {         // todo: add property changed side-effects. descendants can override well.     }      public usstate selectedstate     {         // important: maintain parity between setting property in xaml , procedural code, not touch getter , setter inside dependency property!                 {             return (usstate)getvalue(selectedstateproperty);         }         set         {             setvalue(selectedstateproperty, value);         }     } 

i wasn't able selectedvalue bound property of selectedstate fire, ended hooking selectionchanged event.

    private void cbostate_selectionchanged(object sender, selectionchangedeventargs e)     {         if (e.addeditems?.count > 0)         {             selectedstate = (usstate)e.addeditems[0];         }     } 

in other user control, have in xaml:

<uc:statedropdown margin="10,0,0,0" selectedstate="{binding selectedstate}" ></uc:statedropdown> 

and viewmodel (i'm using caliburn micro), have property:

    protected usstate _selectedstate;     public usstate selectedstate     {         { return _selectedstate; }         set         {             _selectedstate = value;             notifyofpropertychange(() => selectedstate);                         }     } 

the combo populated expected. however, selectedstate never fired/updated when change selection.

i had tried using selecteditem instead of selectedvalue, same results.

i'm sure i'm missing obvious, i'm having trouble seeing went wrong.

edit: here's fixed binding.

i removed selectionchanged event. modified "hosting page" usercontrol set twoway binding:

<uc:statedropdown margin="10,0,0,0" selectedstate="{binding selectedstate, mode=twoway}" ></uc:statedropdown> 

as added that, selectedstate started being updated when changed combobox value.

the things see, line :

 selectedvalue="{binding selectedstate, mode=twoway, relativesource={relativesource ancestortype=usercontrol}}"  

you don't need it, because of selectionchanged event. , can cause problem.

also bind selectedstate of usercontrol using twoway binding.

hope you.


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -