WPF:绑定项目添加到 UserControl 的暴露子级

发布于 2024-08-31 22:11:38 字数 619 浏览 1 评论 0原文

我有一个用户控件,允许通过公开网格的 Children 属性来向其中添加项目。我添加的任何控件都显示正常,但是当我尝试将添加的项目上的属性绑定到主窗口中的控件时,什么也没有发生(示例):

<TextBox Name="txtTest" Text="Success!" />

<mycontrols:CustomUserControl.ExposedGridChildren>
     <TextBox Text="{Binding ElementName=txtTest, Path=Text, FallbackValue=fail}"/>
</mycontrols:CustomUserControl.ExposedGridChildren>

此示例始终导致 TextBox 的文本显示“失败”。以下是我在用户控件中公开子级的方式:

public UIElementCollection ExposedGridChildren
    {
        get { return grdContainer.Children; }
    }

有什么想法吗?是范围问题吗?我知道由于范围错误,我无法命名添加到子级的元素。谢谢,布莱恩。

I have a user control that allows items to be added to it by exposing a Grid's Children property. Any control I add shows up fine but when I try to bind a property on the added item to a control in the main window nothing happens (example):

<TextBox Name="txtTest" Text="Success!" />

<mycontrols:CustomUserControl.ExposedGridChildren>
     <TextBox Text="{Binding ElementName=txtTest, Path=Text, FallbackValue=fail}"/>
</mycontrols:CustomUserControl.ExposedGridChildren>

This example always results in the TextBox's text showing "fail". Here is how I'm exposing the children in the user control:

public UIElementCollection ExposedGridChildren
    {
        get { return grdContainer.Children; }
    }

Any thoughts? Is it a scope issue? I know I can't name the elements I add to the children because of scope errors. Thanks, Brian.

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

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

发布评论

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

评论(2

我不吻晚风 2024-09-07 22:11:38

这可能会回答你的问题:
如何将网格的子级绑定到列表?< /a>

或者正如 WPF 博士所说:(http://drwpf.com/blog/2007/10/15/itemscontrol-a-is-for-abundance/

面板是 ItemsControl 吗?

没有。面板的逻辑子项
是 UIElements,而逻辑
ItemsControl 的子级(其
Items) 可以是任何 CLR 对象。

侧边栏:那么什么是面板?主要
面板的作用是提供布局
对其子女的支持。虽然一个
面板确实维护着一个集合
子元素,从技术上讲它不是
WPF“控件”...也就是说,它不
从 Control 基类派生并且
它不支持 WPF 的概念
模板化。相反,它是一个单一的
具有单一目的的元素……即,
尺寸和位置(又名,测量
并安排)它的孩子。

This might answer your question:
How do you bind a grid's children to a list?

Or as Dr. WPF puts it: (http://drwpf.com/blog/2007/10/15/itemscontrol-a-is-for-abundance/)

Is a Panel an ItemsControl?

No. The logical children of a panel
are UIElements, whereas the logical
children of an ItemsControl (its
Items) can be any CLR objects.

Sidebar: So what is a panel? The main
role of a panel is to provide layout
support for its children. Although a
panel does maintain a collection of
child elements, it is not technically
a WPF “control”… That is, it does not
derive from the Control base class and
it does not support the WPF notion of
templating. Instead, it is a single
element with a single purpose… namely,
to size and position (a.k.a., measure
and arrange) its children.

独夜无伴 2024-09-07 22:11:38

显然我对这件事的处理方式完全错误。我需要做的是创建一个外观较少的控件并将其模板化以具有一个(或多个)内容呈现器。

I was apparently going about this thing all wrong. What I needed to do was create a look-less control and template it to have one (or more) contentpresenter(s).

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