WPF Datagrid 视图标题绑定 [英] WPF Datagrid view header binding

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

问题描述

实际上我面临的问题与标题所说的略有不同.我试着总结下面的问题.

Actually the issue im facing is slight different than what title says. I try to sumarize the issue below.

Class PersonnelViewModel
{

public SelectedPersonnelItem PersonnelItemViewModel;

}

Class PersonnelItemViewModel
{

}

View 的数据上下文是 A 而数据网格的 Item 源是 SelectedPersonnelItem,它是类中的 B 类型属性A.现在我想将网格列绑定到 A 类中定义的属性.但它不起作用,因为网格的上下文是另一个类(B).我该如何解决这个问题?

Data Context of the View is A whereas the Item source of the data grid is SelectedPersonnelItem which is a B type property in the class A. Now i want to bind the grid column to properties defiend within class A. But its not working as the context of the grid is another class (B). How can i solve this issue ?

XAML

<DataGrid Grid.Row="1"
          SelectedItem="{Binding SelectedPersonnelItem}"
          Name="PersonnelGrid"
          ItemsSource="{Binding PersonnelGridData}"
          event:DatagridRowDoubleClickHandler.MethodName="EditRecord"
          CanUserAddRows="False">
  <DataGrid.Columns>
    <DataGridTemplateColumn>
      <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
          <CheckBox IsChecked="{Binding IsItemChecked, UpdateSourceTrigger=PropertyChanged}"></CheckBox>
        </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    <DataGridTextColumn Header="Name/Company"
                        Binding="{Binding Name}" />
    <DataGridTextColumn Header="Qualification"
                        Binding="{Binding Qualification}" />
    <DataGridTextColumn Header="Arrival"
                        Binding="{Binding ArrivalDate}" />
    <DataGridTextColumn Header="Departure"
                        Binding="{Binding DepartureDate}" />
  </DataGrid.Columns>
</DataGrid> 

推荐答案

最后我设法让它工作.如果我直接将属性绑定到列标题,则不会填充该值.

Finally I managed to get it working. If I directly bind the property to the column header its not populating the value.

我必须执行以下操作:

<DataGridTextColumn Binding="{Binding Name}">
    <DataGridTextColumn.Header> 
        <TextBlock Text="{Binding DataContext.MyProp, 
                       RelativeSource={RelativeSource FindAncestor, 
                       AncestorType={x:Type Window}}}" /> 
    </DataGridTextColumn.Header>
</DataGridTextColumn>

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

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