There is something that confuses me about databinding. I'd like to know if binding a datacontextproperty to a CLR object is a correct practice. Why ? Because i'm creating usercontrol specifically designed to display my data. Doing that, I reuse usercontrols inside each others. Let's say i created a usercontrol called 'UC_a' to display 'Class_a' class objects, which have 'Ca_Prop1' properties. Inside it, i have a bound textbox. Its binding path is 'Ca_Prop1' assuming the 'UCa' datacontext is a 'Class_a' object. Let's also say I reuse 'UCa' in 'UC1' and 'UC2' usercontrols which are intended to display respectively 'Class_1' and 'Class_2' objects. 'Class_1' have a 'C1_Prop_a' properties of type 'Class_a', 'Class_2' have a 'C2_Prop_a' properties of type 'Class_a' too. I'd like to be able to get the datacontext of 'UC_a' set on the 'C1_Prop_a' property when it's used in 'UC1' , and on the 'C2_Prop_a' property when it's used in 'UC2' . I see two solutions: 1) Setting it explicitly in the DataContextChanged event of UC1 and UC2. I tried to do that but it doesn't work when I have more than one instance of UC_a in then aggregating controls (UC1 or UC2 in my example). Binding works just once for one instance only. 2) Binding the UC_a Datacontext itself to 'C1_Prop_a' in UC1 and 'C2_Prop_a' in UC2 (I find it odd). What's the best (or the one that works :) ) ? Hope i'm clear enough. Thanx in advance. Patrice
Hi, Pon wrote: > There is something that confuses me about databinding. I'd like to know if > binding a datacontextproperty to a CLR object is a correct practice. Why ? See http://www.beacosta.com/2006/03/why-should-i-use-objectdataprovider.html HTH, Laurent > > > > Because i'm creating usercontrol specifically designed to display my data. > Doing that, I reuse usercontrols inside each others. > > > > Let's say i created a usercontrol called 'UC_a' to display 'Class_a' class > objects, which have 'Ca_Prop1' properties. Inside it, i have a bound > textbox. Its binding path is 'Ca_Prop1' assuming the 'UCa' datacontext is a > 'Class_a' object. > > > > Let's also say I reuse 'UCa' in 'UC1' and 'UC2' usercontrols which are > intended to display respectively 'Class_1' and 'Class_2' objects. 'Class_1' > have a 'C1_Prop_a' properties of type 'Class_a', 'Class_2' have a > 'C2_Prop_a' properties of type 'Class_a' too. > > > > I'd like to be able to get the datacontext of 'UC_a' set on the 'C1_Prop_a' > property when it's used in 'UC1' , and on the 'C2_Prop_a' property when it's > used in 'UC2' . > > > > I see two solutions: > > 1) Setting it explicitly in the DataContextChanged event of UC1 and UC2. I > tried to do that but it doesn't work when I have more than one instance of > UC_a in then aggregating controls (UC1 or UC2 in my example). Binding works > just once for one instance only. > > 2) Binding the UC_a Datacontext itself to 'C1_Prop_a' in UC1 and 'C2_Prop_a' > in UC2 (I find it odd). > > > > What's the best (or the one that works :) ) ? > > > > Hope i'm clear enough. > > > > Thanx in advance. > > > > Patrice > > -- Laurent Bugnion [MVP ASP.NET] Software engineering, Blog: http://www.galasoft-LB.ch PhotoAlbum: http://www.galasoft-LB.ch/pictures Support children in Calcutta: http://www.calcutta-espoir.ch