WPF Datagrid的视图头绑定 [英] WPF Datagrid view header binding

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

问题描述

其实这个问题IM面对的是比标题所说略有不同。
我尝试sumarize跌破发行。

 类PersonnelViewModel
{公共SelectedPersonnelItem PersonnelItemViewModel;}类PersonnelItemViewModel
{}

视图的数据上下文的 A ,而数据网格的项目源的 SelectedPersonnelItem 这是一个 type属性在类 A 。现在我想网格列绑定属性的类内defiend的 A 。但它不工作作为电网的背景是另一个类()。我怎样才能解决这个问题呢?

XAML

 <数据网格Grid.Row =1
          的SelectedItem ={结合SelectedPersonnelItem}
          NAME =PersonnelGrid
          的ItemsSource ={结合PersonnelGridData}
          事件:DatagridRowDoubleClickHandler.MethodName =EditRecord
          CanUserAddRows =FALSE>
  < D​​ataGrid.Columns>
    < D​​ataGridTemplateColumn>
      < D​​ataGridTemplateColumn.CellTemplate>
        <&DataTemplate的GT;
          <复选框器isChecked ={结合IsItemChecked,UpdateSourceTrigger =的PropertyChanged}>< /复选框>
        < / DataTemplate中>
      < /DataGridTemplateColumn.CellTemplate>
    < / DataGridTemplateColumn>
    < D​​ataGridTextColumn标题=名称/公司
                        绑定={结合名称}/>
    < D​​ataGridTextColumn标题=资格
                        绑定={结合资质}/>
    < D​​ataGridTextColumn标题=到达
                        绑定={结合ArrivalDate}/>
    < D​​ataGridTextColumn标题=出发
                        绑定={结合DepartureDate}/>
  < /DataGrid.Columns>
< / DataGrid的>


解决方案

最后,我设法得到它的工作。如果我直接的财产,以该列绑定头它不是填充值。

我不得不这样做如下:

 < D​​ataGridTextColumn绑定={绑定名称}>
    < D​​ataGridTextColumn.Header>
        < TextBlock的文本={结合DataContext.MyProp,
                       的RelativeSource = {的RelativeSource FindAncestor,
                       AncestorType = {X:类型窗口}}}/>
    < /DataGridTextColumn.Header>
< / DataGridTextColumn>

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 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.

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>

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

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