Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > Avalon

Vista - Is binding a datacontext to a CLR object correct ?

 
 
Old 03-12-2007   #1 (permalink)
Pon


 
 

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 SpecsSystem Spec
Old 03-13-2007   #2 (permalink)
Laurent Bugnion [MVP]


 
 

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 SpecsSystem Spec
Old 03-25-2007   #3 (permalink)
Pon


 
 

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 SpecsSystem Spec
 

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46