绑定到当前 ItemsSource 上下文之外的 DataContext [英] Binding to DataContext outside current ItemsSource context

查看:26
本文介绍了绑定到当前 ItemsSource 上下文之外的 DataContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DataSet 绑定到 Window.DataContext;我也有一个 DataGrid:

I have a DataSet bound to the Window.DataContext; I also have a DataGrid:

<DataGrid ItemsSource={Binding Tables[Items]}>
    <DataGrid.Columns>
        <DataGridTextBoxColumn Header={Binding Path=DataContext.Tables[Names]/Test, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}} />
    </DataGrid.Columns>
</DataGrid>

基本上,我试图将该列的标题绑定到数据表名称"、测试"列、第一行.

Basically, I'm trying to bind the Header of that column to DataTable "Names", Column "Test", first row.

但是,我做对了.请注意,我可以在 DataGrid 之外很好地绑定它.Grid 的 ItemsSource 更改了数据上下文,我不知道如何在外部引用原始 DataContext.

However, I can't get it right. Note that I can bind it fine outside the DataGrid. The Grid's ItemsSource changes the data context and I don't know how to refer outside to the original DataContext.

好像绑定成功了;但问题是 Window.DataContext 中 Tables[Names] 的当前项目(第一行)丢失了.

It seems that the binding succeeds; but the problem is that the current item (first row) of the Tables[Names] in the Window.DataContext got lost.

如果我将 DataSet 设为静态并通过 {x:Static local:dataset} 访问它,则一切正常.但是我不能使用静态数据集,因为会有多个实例(多用户).

If I make the DataSet static and access it via {x:Static local:dataset} then things work fine. But I can't use static datasets because there will be multiple instances (multi-user).

有人能指出我正确的方向吗?

Can anyone please point me in the right direction?

推荐答案

我很确定你可以通过使用 RelativeSource 绑定来完成你想做的事情.

I'm pretty sure you could do what you're trying to do by using RelativeSource binding.

<DataGrid ItemsSource="{Binding StringCollection}" 
          AutoGenerateColumns="False">
  <DataGrid.Columns>
    <DataGridTextColumn Binding="{Binding}" />
    <DataGridTextColumn Binding="{
                          Binding RelativeSource={
                            RelativeSource FindAncestor, 
                            AncestorType={x:Type Window}},
                          Path=DataContext.SomethingOutsideDataContext}" />
  </DataGrid.Columns>
</DataGrid>

我在以下位置做了一个简单的例子:http://bitbucket.org/claus/wpf-bindingoutsidedatacontext

I made a quick example at: http://bitbucket.org/claus/wpf-bindingoutsidedatacontext

它允许你绑定到父窗口,在我的例子中,它有视图模型作为数据上下文(上面有SomethingOutsideDataContext属性).

It allows you to bind to the parent Window, which in my case has the viewmodel as datacontext (with the SomethingOutsideDataContext property on it).

您应该知道,这仅适用于 WPF 而不适用于 Silverlight - Silverlight 中尚未实现FindAncestor, AncestorType"的东西...我不确定是否有另一种方法,除了使用静态资源.

You should be aware though, this will only work with WPF and not Silverlight - the 'FindAncestor, AncestorType' stuff has not been implemented in Silverlight yet... I'm not sure if there's another method, besides using static resources.

这篇关于绑定到当前 ItemsSource 上下文之外的 DataContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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