WPF MVVM Bind User Control to Main Window View Model -


i asked similar question while here wpf mvvm user control. got answers, way off, guess wasn't clear in want do....

i working on wpf application using mvvm. app built using component based approach, have user controls i've defined used throughout application. example, have address control. want use multiple places throughout application. here's example. see here:

http://sdrv.ms/1ad775h

the part green border address control. control has own view model.

when place on window or other control, need tell pk of customer load addresses for. created customer id dependencyproperty:

public partial class addressview : usercontrol {     public addressview()     {         initializecomponent();     }      public static dependencyproperty customeridproperty = dependencyproperty.register("customerid", typeof(int), typeof(addressview),         new uipropertymetadata(0, addressview.customeridpropertychangedcallback, addressview.customeridcoerce, true));       public int customerid     {         // these never fire         { return (int)getvalue(customeridproperty); }         set { setvalue(customeridproperty, value); }     }      private static void customeridpropertychangedcallback(dependencyobject d, dependencypropertychangedeventargs args)     {         // never fires         addressview instance = (addressview)d;         instance.customerid = (int)args.newvalue;     }     enter code here     private static object customeridcoerce(dependencyobject d, object value)     {         return value;   // <=== fires on startup     } } 

then in mainwindowview have:

<vw:addressview grid.row="1"                 grid.column="0"                 x:name="addresslist"                 customerid="{binding elementname=themainwindow, path=selectedcustomer.id, mode=twoway}"/> 

note comments in user control's cs. coerce fires on startup. callback never fires, nor customerid getter or setter.

what happen seems simple, can't make work....

when customer selected customerid should passed address usercontrol. in vm address usercontrol should handle getting & saving data.

so, again, 2 questions:

1) see what's wrong?

2) how usercontrol dp send pk viewmodel?

if anyone's interested, sample project here: http://sdrv.ms/136bj91

thanks

try :

 customerid="{binding relativesource={relativesource findancestor,  ancestortype={x:type window}}, path=datacontext.yourselecteditem.theproperty}" 

i not sure how manage seleted item in window, please change yourselecteditem.theproperty accordingly.

thaks


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -