将DataGridColumns标题绑定到窗口的DataContext [英] Bind DataGridColumns Header to DataContext of the window

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

问题描述

我有一个DataGrid,想将Header -property绑定到我的Windows DataContext的属性,但是我没有使它工作.

绑定对我来说可能是一件痛苦的事情,因为仅仅使用Binding时,永远都不知道this具有哪个上下文. 我知道Binding={}中的上下文"是DataGrid s ItemsSource的单个元素.但是Header={Binding ???}的上下文"是什么?

我已经尝试过:

Header="{Binding Path=DataContext.MyProperty, RelativeSource={RelativeSource Self}}
Header="{Binding Path=DataContext.MyProperty, RelativeSource={RelativeSource TemplatedParent}}
Header="{Binding Path=DataContext.MyProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MyWindow}}}
Header="{Binding Path=DataContext.MyProperty, ElementName=MyWindowName}

我尝试使用和不使用Path,但是没有任何效果.

例如,将最后一个与ElementName一起使用,我得到以下绑定例外:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=DataContext.MyProperty; DataItem=null; target element is 'DataGridTextColumn' (HashCode=51072575); target property is 'Header' (type 'Object')

是否有任何工具可以在运行时检查/更改绑定?甚至不知道当前的上下文"是什么?

注意:DataGrid在Mahapps.Flyout内部(不确定是否有话要说).

解决方案

由于DataGridTextColumn或任何其他受支持的数据网格列都不属于datagrid可视化树的一部分,因此它们不继承datagrid.由于它们不位于可视树中,因此使用RelativeSource获取DataContext的任何尝试均将无效.

解决方案-您可以创建一个proxy元素来绑定window/control的数据上下文;使用该代理元素绑定DataGridTextColumn的标头.

<Grid>
   <Grid.Resources>
       <FrameworkElement x:Key="ProxyElement" DataContext="{Binding}"/>
   </Grid.Resources>
       <ContentControl Visibility="Collapsed" Content="{StaticResource ProxyElement}"></ContentControl>
       <DataGrid  
                       ItemsSource="{Binding Collection}" 
                       AutoGenerateColumns="False">
          <DataGrid.Columns>
              <DataGridTextColumn Header="{Binding DataContext.MyProperty, Source={StaticResource ProxyElement}}" Binding="{Binding PropertyName}" />
          </DataGrid.Columns>
     </DataGrid>
</Grid>

I have a DataGrid and want to bind the Header-property to a property of my windows DataContext but I did not get it working.

Binding can be such a pain as (for me) it is never clear which context this has when simply using Binding. I know that the "Context" in the Binding={} is a single element of the DataGrids ItemsSource. But what is the "Context" for Header={Binding ???}?

I've already tried:

Header="{Binding Path=DataContext.MyProperty, RelativeSource={RelativeSource Self}}
Header="{Binding Path=DataContext.MyProperty, RelativeSource={RelativeSource TemplatedParent}}
Header="{Binding Path=DataContext.MyProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MyWindow}}}
Header="{Binding Path=DataContext.MyProperty, ElementName=MyWindowName}

I tried with and without Path but nothing is working.

For example, using the last one with ElementName I get the following binding-exception:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=DataContext.MyProperty; DataItem=null; target element is 'DataGridTextColumn' (HashCode=51072575); target property is 'Header' (type 'Object')

Is there any tool to check/change bindings at runtime? Or even to know what the current "Context" is?

Note: The DataGrid is inside a Mahapps.Flyout (not sure if this has something to say).

解决方案

Since DataGridTextColumn or any other supported data grid columns are not part of visual tree of datagrid so they don't inherit the DataContext of datagrid. Since, they don't lie in visual tree so any try to get DataContext using RelativeSource won't work.

Solution - You can create a proxy element to bind the data context of window/control; use that proxy element to bind the Header of DataGridTextColumn.

<Grid>
   <Grid.Resources>
       <FrameworkElement x:Key="ProxyElement" DataContext="{Binding}"/>
   </Grid.Resources>
       <ContentControl Visibility="Collapsed" Content="{StaticResource ProxyElement}"></ContentControl>
       <DataGrid  
                       ItemsSource="{Binding Collection}" 
                       AutoGenerateColumns="False">
          <DataGrid.Columns>
              <DataGridTextColumn Header="{Binding DataContext.MyProperty, Source={StaticResource ProxyElement}}" Binding="{Binding PropertyName}" />
          </DataGrid.Columns>
     </DataGrid>
</Grid>

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

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