开发者

How to override data context in stack panel?

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

  Class A
    {

    开发者_JS百科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>


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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜