如何覆盖堆栈面板中的数据上下文?

发布于 2024-10-13 03:11:16 字数 621 浏览 7 评论 0原文

如何覆盖第一个内部堆栈面板的数据上下文...以便..我可以引用 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-10-20 03:11:16
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>
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>
风吹短裙飘 2024-10-20 03:11:16

有几种方法取决于您的课程的实际设置方式。

  1. 创建引用对象父类的关联属性。如果 B 类是 A 类的子类,则 B 类中可以有一个属性引用包含该 B 类的 A 对象。当您的 DataContext 在顶级堆栈面板上设置时,您的第一个子级可以具有类似 DataContext="{Binding Parent}" 的内容,其中父级是引用 ClassA 的属性。

  2. 只需将顶级堆栈面板中的 DataContext 设置为 ClassA 绑定,将第一个子项留空,并将其设置为每个连续堆栈面板的 b 属性。

如果我理解您的权利,请告诉我。我可能需要更多有关您的程序和问题的背景信息。

此外,复制/粘贴真实代码比直接键入要好。真实的代码通常会为问题提供更好的上下文。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文