Windows Vista Forums

Is binding a datacontext to a CLR object correct ?

  1. #1


    Pon Guest

    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

  2. #2


    Laurent Bugnion [MVP] Guest

    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

  3. #3


    Pon Guest

    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

Is binding a datacontext to a CLR object correct ?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Binding TextBox.Text to property in nested (multi levels) object withbindingsource, Error if grandchild object is NULL Efy .NET General 0 14 Oct 2009
Object Binding Query Julian .NET General 0 21 Dec 2008
WPF - Binding IsEnabled property doesnt evaluate until DataContext Todd Beaulieu .NET General 1 06 Mar 2008
How to reset datacontext to parent datacontext Pon Avalon 3 25 Oct 2006
Binding to same DP on another instance of same object won't work. Marcus Avalon 0 15 Mar 2006