【问题标题】:Silverlight: Nested containers and BindingSilverlight:嵌套容器和绑定
【发布时间】:2009-07-16 12:12:26
【问题描述】:

场景位:

在我的 Silverlight 应用程序中的一个控件上,我嵌入了一个自定义用户控件。在这个嵌入式控件中是另一个包含数据网格的自定义用户控件。我想使用绑定来填充数据网格。很简单,我只是在父控件的 DataContext 中指定一个集合。

父表单:

<UserControl x:Class="ParentControl"
             ...>
    <Grid x:Name="LayoutRoot" >        
        <ReusableControl />
    </Grid>
</UserControl>

父代码隐藏:

public partial class ParentControl: UserControl 
    {

        public ParentControl()
        {
            InitializeComponent();
            this.DataContext = ObjectCollection;
        }

        public ObservableCollection<object> ObjectCollection
        {
            get ;
            set ;
        }
    }

中间形式

<UserControl x:Class="ReusableControl"
             ...>
    <Grid x:Name="LayoutRoot" Background="Gold">        
        <CustomDataGrid />
    </Grid>
</UserControl>

儿童控制:

<UserControl x:Class="CustomDataGrid"
             ...>
    <Grid x:Name="LayoutRoot">        
        <data:DataGrid x:Name="dgItems"
                       AutoGenerateColumns="True"
                       ItemsSource="{Binding ObjectCollection}"
                       >
        </data:DataGrid>
    </Grid>
</UserControl>

问题位:

我想根据父控件 DataContext 中的另一个集合动态指定数据网格的列。我怎样才能做到这一点?给这只猫剥皮的方法不止一种吗?*

谢谢, 标记

*在提出这个问题时没有猫受到伤害。

【问题讨论】:

    标签: silverlight binding nested


    【解决方案1】:

    经过几个小时后,我找到了一个解决方法,我已发布 here。这在我看来并不是世界上最好的解决方案,但它可以工作,并且不需要在整个应用程序中注册事件处理程序。它也可以自上而下地工作,这正是我想要的。

    我怀疑我可以更好地使用依赖属性,以防止在同一类中需要 DP 和 NP,但我没时间了 :-(

    希望这对其他人有所帮助。

    【讨论】:

      猜你喜欢
      • 2011-05-04
      • 2011-08-22
      • 2011-08-24
      • 2011-09-03
      • 1970-01-01
      • 2011-05-05
      • 1970-01-01
      • 2017-08-25
      • 1970-01-01
      相关资源
      最近更新 更多