WPF:绑定项目添加到 UserControl 的暴露子级
我有一个用户控件,允许通过公开网格的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能会回答你的问题:
如何将网格的子级绑定到列表?< /a>
或者正如 WPF 博士所说:(http://drwpf.com/blog/2007/10/15/itemscontrol-a-is-for-abundance/)
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/)
显然我对这件事的处理方式完全错误。我需要做的是创建一个外观较少的控件并将其模板化以具有一个(或多个)内容呈现器。
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).