Silverlight:如何绑定到父视图的 DataContext? [英] Silverlight: How to bind to parent view's DataContext?

查看:27
本文介绍了Silverlight:如何绑定到父视图的 DataContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 childView 的 ParentView

I have a ParentView that contains a childView

<UserControl ... x:Name="MyParentView">
     <Grid>
        <sdk:TabControl Name="ContactTabControl">
            <sdk:TabItem Header="Contact" Name="CustomerTabItem">
                <Grid>
                    <Views:CustomerView/>
                </Grid>
            </sdk:TabItem>
        </sdk:TabControl>
    </Grid>
</UserControl>

在我的 CustomerView 中,我想将 Firstname 文本框绑定到 Parent 的 DataContext.我在 CustomerView 中试过这个:

Within my CustomerView I would like to bind the Firstname textbox to Parent's DataContext. I have tried this inside the CustomerView:

<TextBox Text={Binding ElementName=MyParentView, Path=DataContext.Firstname} />

我感觉 CustomerView 根本看不到它的父级,因此永远找不到 ElementName "MyParentView".

I have the feeling that CustomerView won't be able to see its parent at all, hence the ElementName "MyParentView" would never be found.

您对此有何建议?

推荐答案

我做过类似的事情,但我只是将它直接绑定到 Path 考虑到如果我不给它明确的数据上下文,它将查找层次结构并找到一个匹配的.

I've done a similar thing but I just bound it directly to Path considering that if I don't give it explicit data context, it will lookup the hierarchy and find one that matches.

所以这应该可以满足您的需求:

So this should get you what you want:

<TextBox Text={Binding Path=FirstName} />

如果您需要指定显式数据上下文,您可以随时执行:

if you need to specify explicit datacontext you can always do:

<Grid>
    <Views:CustomerView DataContext={"CustomContextHere"}/>
</Grid>

这篇关于Silverlight:如何绑定到父视图的 DataContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆