简体   繁体   English

Silverlight - XAML 中的相关数据上下文?

[英]Silverlight - relative DataContext in XAML?

In Silverlight XAML, I think I've just realized that a DataContext declaration on a nested container is relative to the parent container's DataContext.在 Silverlight XAML 中,我想我刚刚意识到嵌套容器上的 DataContext 声明与父容器的 DataContext 相关。 Can you all please confirm.请大家确认一下。

If so, then let me ask this: On a child XAML container element (ie StackPanel) how would you would jump out of that relative DataContext tree, and start at a higher place, or a start a different DataContext all together if you wanted set the DataContext on the StackPanel to a different root context?如果是这样,那么让我问这个问题:在一个子 XAML 容器元素(即 StackPanel)上,你将如何跳出相关的 DataContext 树,并从更高的位置开始,或者如果你想设置一个不同的 DataContext 一起开始StackPanel 上的 DataContext 到不同的根上下文?

In other words, how to break the child DataContext free of the parent DataContext?换句话说,如何从父 DataContext 中打破子 DataContext?

(Looking for XAML code solution/syntax) (寻找 XAML 代码解决方案/语法)

Your first assumnption is correct.你的第一个假设是正确的。 The DataContext is kind of inherited by the nested elements. DataContext 是由嵌套元素继承的。

On a child XAML container element you can always redefine what the DataContext is.在子 XAML 容器元素上,您始终可以重新定义 DataContext 是什么。

See example below:请参见下面的示例:


    <UserControl.Resources>
        <local:Customer x:Key="Cust">
        <local:Supplier x:Key="Supp">
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="White" DataContext="{StaticResource Cust}">
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal" Grid.Row="0">
            <TextBlock Text="Customer Name: " />
            <TextBox Text="{Binding Path=Name}"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal" Grid.Row="1" DataContext="{StaticResource Supp}">
            <TextBlock Text="Supplier Name: " />
            <TextBox Text="{Binding Path=Name}"/>
            <TextBlock Text=" Telephone: " />
            <TextBox Text="{Binding Path=Telephone}"/>
        </StackPanel>
    </Grid>

And here are the "Model" classes for the example above:以下是上述示例的“模型”类:


    public class Customer
    {
        public Customer()
        {
            Name = "Customer name";
            Address = "Customer address";
        }
        public string Name { get; set; }
        public string Address { get; set; }
    }

    public class Supplier
    {
        public Supplier()
        {
            Name = "Supplier name";
            Address = "Supplier address";
            Telephone = "(555)555-5555";
        }

        public string Name { get; set; }
        public string Address { get; set; }
        public string Telephone { get; set; }
    }

Check out this blog, it details a proxy class for doing what you need all from xaml.查看此博客,它详细介绍了一个代理 class 用于从 xaml 完成您需要的所有操作。

[ http://weblogs.asp.net/dwahlin/archive/2009/08/20/creating-a-silverlight-datacontext-proxy-to-simplify-data-binding-in-nested-controls.aspx][1] [ http://weblogs.asp.net/dwahlin/archive/2009/08/20/creating-a-silverlight-datacontext-proxy-to-simplify-data-binding-in-nested-controls.aspx][1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM