binding - How do I make my WPF User Control's dependency properties update my view model? -


i'm trying create user control dependency properties bind to. internally have combobox bound these same properties, binding works 1 way. combobox fills itemssource, selecteditem doesn't updated viewmodel i'm binding to.

a simplified example:

this view model bind user control:

public class peopleviewmodel : inotifypropertychanged {     public peopleviewmodel()     {         people = new list<string>( new [] {"john", "alfred","dave"});         selectedperson = people.firstordefault();     }      public event propertychangedeventhandler propertychanged;      private ienumerable<string> _people;     public ienumerable<string> people      {          { return _people; }          set         {             _people = value;             if (propertychanged != null)             {                 propertychanged(this, new propertychangedeventargs("people"));             }         }     }      private string _selectedperson;     public string selectedperson      {         { return _selectedperson; }         set          {              _selectedperson = value;             if (propertychanged != null)             {                 propertychanged(this, new propertychangedeventargs("selectedperson"));             }         }      } } 

this user control:

<usercontrol x:class="peoplecontroltest.peoplecontrol"          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"           mc:ignorable="d" d:designheight="56" d:designwidth="637"> <stackpanel >     <combobox margin="11"               itemssource="{binding boundpeople, relativesource={relativesource ancestortype=usercontrol}}"               selecteditem="{binding boundselectedperson, relativesource={relativesource ancestortype=usercontrol}}"/> </stackpanel> 

with code behind

public partial class peoplecontrol : usercontrol {     public peoplecontrol()     {         initializecomponent();     }      public static readonly dependencyproperty boundpeopleproperty =         dependencyproperty.register("boundpeople", typeof(ienumerable<string>), typeof(peoplecontrol), new uipropertymetadata(null));      public static readonly dependencyproperty boundselectedpersonproperty =         dependencyproperty.register("boundselectedperson", typeof(string), typeof(peoplecontrol), new uipropertymetadata(""));      public ienumerable<string> boundpeople     {         { return (ienumerable<string>)getvalue(boundpeopleproperty); }         set { setvalue(boundpeopleproperty, value); }     }      public string boundselectedperson     {         { return (string)getvalue(boundselectedpersonproperty); }         set { setvalue(boundselectedpersonproperty, value); }     } } 

and how bind user control in main window (with windows data context set instance of viewmodel)

<window x:class="peoplecontroltest.mainwindow"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"           xmlns:controls="clr-namespace:peoplecontroltest"     title="mainwindow" height="350" width="525">     <grid>         <controls:peoplecontrol              boundpeople="{binding people}"              boundselectedperson="{binding selectedperson}"/>     </grid> </window> 

the combobox in user control fills names, when select different name doesn't updated view model. idea i'm missing here?

thanks!

some properties bind two-way default (including selecteditem) boundselectedperson not. can set mode of binding:

   <controls:peoplecontrol              boundpeople="{binding people}"              boundselectedperson="{binding selectedperson, mode=twoway}"/> 

or can make twoway default setting flag on dependencyproperty:

    public static readonly dependencyproperty boundselectedpersonproperty =         dependencyproperty.register("boundselectedperson", typeof(string), typeof(peoplecontrol), new frameworkpropertymetadata("",frameworkpropertymetadataoptions.bindstwowaybydefault)); 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -