Silverlight:如何绑定到父视图的DataContext?

发布于 2024-10-17 19:48:31 字数 761 浏览 1 评论 0原文

我有一个 ParentView,其中包含一个 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 中尝试过此操作:

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

我感觉 CustomerView 根本无法看到其父级,因此永远找不到 ElementName“MyParentView”。

您对此有何建议?

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>

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} />

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

What is your advice on this?

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

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

发布评论

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

评论(2

纵山崖 2024-10-24 19:48:31

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

因此,这应该可以满足您的需求:

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

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

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

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>
旧情别恋 2024-10-24 19:48:31

Maverik 的替代解决方案是:

1 在客户视图中定义依赖属性:

public partial class CustomerView : UserControl
    {
        public CustomerView()
        {
            InitializeComponent();
        }

        public static DependencyProperty FirstNameProperty =
            DependencyProperty.Register("FirstName", typeof(string), typeof(CustomerView), new PropertyMetadata(string.Empty, CustomerView.FirstNameChanged));

        public string FirstName
        {
            get { return (string)GetValue(FirstNameProperty); }
            set { SetValue(FirstNameProperty, value); }
        }

        private static void FirstNameChanged(object sender, DependencyPropertyChangedEventArgs e)
        { }
    }

2 修改客户视图的文本框以绑定到此依赖属性(注意元素绑定“this”)

<UserControl x:Class="SLApp.CustomerView"
    x:Name="this"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <TextBox Text="{Binding Path=FirstName, ElementName=this, Mode=TwoWay}"/>
    </Grid> </UserControl>

3 修改父视图并将其 DataContext 绑定到新视图依赖属性

<sdk:TabControl Name="ContactTabControl">
                        <sdk:TabItem Header="Contact" Name="CustomerTabItem">
                            <Grid>
                                <local:CustomerView FirstName="{Binding ElementName=ContactTabControl, Path=DataContext}"/>
                            </Grid>
                        </sdk:TabItem>
                    </sdk:TabControl>

4 设置父级的 DataContext

public partial class MyParentView : UserControl
    {
        public MyParentView()
        {
            InitializeComponent();

            ContactTabControl.DataContext = "A name";
        }
    }

瞧,它可以工作了。不是最优雅的解决方案,但它可以完成您的场景的工作

An alternative solution to Maverik's is :

1 Define a dependency property in your customer view :

public partial class CustomerView : UserControl
    {
        public CustomerView()
        {
            InitializeComponent();
        }

        public static DependencyProperty FirstNameProperty =
            DependencyProperty.Register("FirstName", typeof(string), typeof(CustomerView), new PropertyMetadata(string.Empty, CustomerView.FirstNameChanged));

        public string FirstName
        {
            get { return (string)GetValue(FirstNameProperty); }
            set { SetValue(FirstNameProperty, value); }
        }

        private static void FirstNameChanged(object sender, DependencyPropertyChangedEventArgs e)
        { }
    }

2 Modify the customer view's textbox to bind to this dependency property (note the element binding "this")

<UserControl x:Class="SLApp.CustomerView"
    x:Name="this"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <TextBox Text="{Binding Path=FirstName, ElementName=this, Mode=TwoWay}"/>
    </Grid> </UserControl>

3 Modify the parent view and bind it's DataContext to the new dependency property

<sdk:TabControl Name="ContactTabControl">
                        <sdk:TabItem Header="Contact" Name="CustomerTabItem">
                            <Grid>
                                <local:CustomerView FirstName="{Binding ElementName=ContactTabControl, Path=DataContext}"/>
                            </Grid>
                        </sdk:TabItem>
                    </sdk:TabControl>

4 Set the parent's DataContext

public partial class MyParentView : UserControl
    {
        public MyParentView()
        {
            InitializeComponent();

            ContactTabControl.DataContext = "A name";
        }
    }

Voila' it works. Not the most elegant solution but it gets the job done for your scenario

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