WPF Datagrid 视图标题绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16653005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:49:52  来源:igfitidea点击:

WPF Datagrid view header binding

c#wpfbindingheadergrid

提问by Kurubaran

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
{

}

Data Context of the View is Awhereas the Item source of the data grid is SelectedPersonnelItemwhich is a Btype 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 ?

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

XAML

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> 

回答by cheedep

You can access the parents datacontext using

您可以使用访问父母数据上下文

Binding="{Binding RelativeSource={RelativeSource FindAncestor, 
    AncestorType={x:Type DataGrid}}, Path=DataContext.ColumnName}" 

So to add a column showing A's Property do

所以要添加一列显示 A 的属性做

<DataGridTextColumn Header="Company" Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.Company}"/>

回答by Kurubaran

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

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

I had to do as following:

我必须这样做:

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