从子控件绑定到主数据上下文
假设我有一个显示家庭
信息的用户控件。
该用户控件的边框的一个文本块(其数据上下文是Father
)必须显示家庭名称。
问题是我在代码隐藏中绑定了 datacontext:
this.DataContext=this;
实际上这个代码隐藏有近 4k 行代码,这就是我目前无法将其转换为 MVVM 的原因。
因此,在代码隐藏中,我有诸如 Father
、Mother
和 FamilyName
之类的属性
,我需要在文本块中显示 FamilyName
以 Father
作为 DataContext 的边框。
我该怎么做呢?
Let's say I have a usercontrol which displays Family
information.
One textblock of border(which datacontext is Father
) of this usercontrol has to display name of family.
The problem is that I have binded datacontext in codebehind:
this.DataContext=this;
Actually this codebehind has almost 4k lines of codes, and tis is reason I can't transform it to MVVM
at this moment.
So in the codebehind I have so properties like Father
, Mother
and FamilyName
and I need display FamilyName
in textblock in border which has Father
as DataContext.
How can I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要做的是相对祖先绑定,这将在 Silverlight 5 中出现。(即:父亲是 DataContext 的成员,并且您想要访问 DataContext 的另一个子级。)。
作为解决方法,您可以在 Page.Resources 中将 FamilyName 声明为 StaticResource,然后绑定到该 StaticResource。我不确定绑定是否能正常工作。
或者你没有将整个父控件绑定到Father。绑定到单个元素,例如
{Binding Path=Father.FirstName}
What you want to be able to do is relative ancestor binding, which is coming up in Silverlight 5. (ie: Father is a member of the DataContext, and you want to access another children of the DataContext.).
As a workaround, you can declare FamilyName as a StaticResource in your Page.Resources and then bind to the StaticResource. I'm not sure if the bindings would work correctly.
Or you don't bind the whole parent control to Father. Bind to the individual elements like
{Binding Path=Father.FirstName}