WPF绑定到家长的DataContext [英] WPF Binding to parent DataContext

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

问题描述

我们有一个标准的MVVM模式一个WPF应用程序,利用束带(因此MefedMVVM)为查看 - >视图模型的分辨率。这种运作良好,我可以在相关的控件绑定到的视图模型属性。

We have a WPF application with a standard MVVM pattern, leveraging Cinch (and therefore MefedMVVM) for View -> ViewModel resolution. This works well, and I can bind the relevant controls to properties on the ViewModel.

在一个特定的景观,我们有一个Infragistics的XamGrid。此网格是绑定到视图模型一个ObservableCollection,并显示相应的行。不过,我当时对这个网格中哪个我想一个TextBox文本值绑定到父DataContext的,而不是一个的ObservableCollection属性的特定列。这种结合是失败的。

Within a particular View, we have an Infragistics XamGrid. This grid is bound to an ObservableCollection on the ViewModel, and displays the appropriate rows. However, I then have a specific column on this grid which I am trying to bind a TextBox text value to a property on the parent DataContext, rather than the ObservableCollection. This binding is failing.

我们已经通过几个选项,包括在这里了:

We've gone through several options here including:


  1. 使用AncestorType跟踪了树,并绑定到用户控件家长的DataContext的像这样(从的伟大的答案这个问题,以及<一个href=\"http://stackoverflow.com/questions/998092/using-the-parents-datacontext-wpf-dynamic-menu-command-binding\">this 之一)...

  1. Using AncestorType to track up the tree and bind to the DataContext of the parent UserControl like so (from the great answer to this question, as well as this one)...

{Binding Path=PathToProperty, RelativeSource={RelativeSource AncestorType={x:Type typeOfAncestor}}}


  • 指定的ElementName并试图直接针对最高级别的控制。有看看这里如果您想了解使用的ElementName。

  • Specifying the ElementName and trying to target the top level control directly. Have a look here if you'd like to read about using ElementName.

    在使用的资源定义的用户控件尝试和传递的背景下为需要代理FrameorkElement。我们定义如下元素,然后引用作为静态资源...

    Using a 'proxy' FrameorkElement defined in the resources for the UserControl to try and 'pass in' the context as required. We define the element as below, then reference as a static resource...

    <FrameworkElement x:Key="ProxyContext" DataContext="{Binding Path=DataContext, RelativeSource={RelativeSource Self}}"></FrameworkElement>
    


  • 在这种情况下,结合发现FrameworkElement的,但(指定路径时)不能访问任何事情。

    In this case the binding finds the FrameworkElement, but can not access anything beyond that (when specifying a Path).

    读完各处,它看起来很可能这是由Infragistics的XamGrid建筑物列树的外侧引起的。然而,即使是这样的情况下,至少选择2个或3应该工作

    Having read around, it looks quite likely that this is caused by the Infragistics XamGrid building columns outside of the tree. However, even if this is the case, at least options 2 or 3 should work.

    我们的最后的想法是,它关系到V - VM约束力,但即使使用史努比我们还没有找到确切的问题是什么。我绝不是用WPF绑定,所以任何指针将AP preciated的专家。

    Our last thoughts are that it is related to the V - VM binding, but even using Snoop we've yet to find what the exact issue is. I'm by no means an expert with WPF binding so any pointers would be appreciated.

    编辑:我已经发现了一些模板例子来自Infragistics的<一个href=\"http://help.infragistics.com/Help/NetAdvantage/WPF/2012.1/CLR4.0/html/xamGrid_Custom_GroupBy_Row_Display.html\">here我会尽力。

    I have found some templating examples from Infragistics here that I will try.

    编辑2:由于@Dtex指出,模板是要走的路。下面是一个使用XamGrid的相关片段:

    EDIT 2: As pointed out by @Dtex, templates are the way to go. Here is the relevant snippet for use with a XamGrid:

    <ig:GroupColumn Key="CurrentDate">
                    <ig:GroupColumn.HeaderTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=DataContext.CurrentDateTest, RelativeSource={RelativeSource AncestorType=UserControl}}" />
                        </DataTemplate>
                    </ig:GroupColumn.HeaderTemplate>
                    <ig:GroupColumn.Columns>
    

    我已经离开了XML开放......你只需添加你想要的列,然后关闭掉相关的标签。

    I've left the XML open... you'd simply add the columns you wanted, then close off the relevant tags.

    推荐答案

    我不知道关于 XamGrid 但是这就是我会使用WPF <$ C $标准做C>的DataGrid :

    I dont know about XamGrid but that's what i'll do with a standard wpf DataGrid:

    <DataGrid>
        <DataGrid.Columns>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding DataContext.MyProperty, RelativeSource={RelativeSource AncestorType=MyUserControl}}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding DataContext.MyProperty, RelativeSource={RelativeSource AncestorType=MyUserControl}}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
    

    由于的TextBlock 文本框在单元格中的模板指定将成为可视化树的一部分,您可以走上去找到你所需要的任何控制权。

    Since the TextBlock and the TextBox specified in the cell templates will be part of the visual tree, you can walk up and find whatever control you need.

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

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