Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
Windos
win32.3rdparty
win32.directx.audio
win32.directx.ddk
win32.directx.graphics
win32.directx.input
win32.directx.managed
win32.directx.misc
win32.directx.networking
win32.directx.sdk
win32.directx.video
win32.dirx.grap.shaders
win32.gdi
win32.international
win32.kernel
win32.messaging
win32.mmedia
win32.networks
win32.ole
win32.rtc
win32.tapi
win32.tapi.beta
win32.tools
win32.ui
win32.wince
win32.wmi
windows.mediacenter
winfx.aero
winfx.announcements
winfx.avalon
winfx.collaboration
winfx.fundamentals
winfx.general
winfx.indigo
winfx.sdk
winfx.winfs
  
 
date: Mon, 12 Mar 2007 17:17:37 +0100,    group: microsoft.public.windows.developer.winfx.avalon        back       


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
date: Mon, 12 Mar 2007 17:17:37 +0100   author:   Pon

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-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
date: Tue, 13 Mar 2007 14:27:27 +0100   author:   Laurent Bugnion [MVP]

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us