![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
| |
| | #1 (permalink) |
| | Is binding a datacontext to a CLR object correct ? 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 |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Is binding a datacontext to a CLR object correct ? 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-...aprovider.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 |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Is binding a datacontext to a CLR object correct ? So binding datacontext between each others is perfectly correct despite of what Blend say. It works perfectly in my code know. Merci Laurent. "Laurent Bugnion [MVP]" <galasoft-lb@bluewin.ch> wrote in message news:%235yLZNXZHHA.3520@TK2MSFTNGP04.phx.gbl... > 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-...aprovider.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 |
My System Specs![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Binding to a User Object with Alternate Credentials | VB Script | |||
| Object Binding Query | .NET General | |||
| WPF - Binding IsEnabled property doesnt evaluate until DataContext | .NET General | |||