-1

How to override the first inner stack panel's data context...So that..I can refer the properties in class A

  Class A
    {

    public B  b;

    }

    Class B
    {
    }

    <stack panel DataContext = b >
        <stack panel>
         // HEre I use properties from class A
        </stack panel>
        <stack panel>
         // HEre I use properties from class B
        </stack panel>
        <stack panel>
         // HEre I use properties from class B
        </stack panel>
        <stack panel>
         // HEre I use properties from class B
        </stack panel>
    </stack panel>
Relativity
  • 6,690
  • 22
  • 78
  • 128

2 Answers2

3
Class A
    {

    // B Should b a property
    public B  B{get; set;};

    }

    Class B
    {
    }

   <!-- Set A's Context here -->
   <StackPanel DataContext=A>
        <StackPanel>
         // HEre you use properties from class A
        </StackPanel>

        <StackPanel DataContext={Binding Path=B}>
         // HEre you use properties from class B
        </StackPanel>

        <StackPanel DataContext={Binding Path=B}>
         // HEre you use properties from class B
        </StackPanel>

        <StackPanel DataContext={Binding Path=B}>
         // HEre you use properties from class B
        </StackPanel>
   </StackPanel>
Prince Ashitaka
  • 8,623
  • 12
  • 48
  • 71
0

Couple of ways depending on how your classes are actually set up.

  1. Create an association property that refers to the parent class of an Object. If Class B is a child of class A, then there can be a property in Class B that refers to which A object contains it. When your DataContext is set on your top level stackpanel, your first child can have something like DataContext="{Binding Parent}" where parent is the property that refers to ClassA.

  2. Just set your DataContext in the top level stackpanel to the ClassA binding, leave it blank for the first child, and set it to your b property for each successive stackpanel.

Please let me know if I understood your right. I may need some more background on your program and problem.

Also, it is better to copy/paste real code than type it out as you go. Real code often gives better context to the problem.

CodeWarrior
  • 7,388
  • 7
  • 51
  • 78
  • What ever you have given as answer...I'm okay with it (And I'm using the 2nd method already). But I want a better solution that's y I post this question. I am using unity...and in xaml file I'm mapping a viewmodel for a given view. And that view model is class A. And in class A I have property of type B. If I have a property in B which is of type A, it will solve the probem. But my question is Do we have something like "this in C# file" that we can use in XAML ?..so that "this" will point to current viewmodel. – Relativity Jan 21 '11 at 19:32
  • Ah! I got it now. You want to have a way to call back from other places in a view to the "current ViewModel". The only mechanism I have seen for that is the DataContextSpy object that Josh Smith created which can be placed in an elements resources and can report that elements DataContext to other parts of the program. Take a look at this link: http://blogs.infragistics.com/blogs/josh_smith/archive/2008/06/26/data-binding-the-isvisible-property-of-contextualtabgroup.aspx – CodeWarrior Jan 22 '11 at 15:21