绑定不能在WPF Datagrid第二行头 [英] Binding not working in WPF Datagrid second row header

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

问题描述

我的Xaml -

My Xaml-

 <Grid>
        <DataGrid Name="DataGrid" AutoGenerateColumns="False">
            <DataGrid.RowHeaderTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
                        <TextBlock Text="19" />
                        <TextBlock Text="{Binding Path=Items.Year, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=DataGridRowHeader}}" Foreground="#9493CF" FontSize="16" />                        
                    </StackPanel>
                </DataTemplate>
            </DataGrid.RowHeaderTemplate>
            <DataGrid.Columns>                
                <DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" />
                <DataGridTextColumn Header="Course" Binding="{Binding Path=Course}" />
            </DataGrid.Columns>        
        </DataGrid>
    </Grid>

在C#

  var itemList = new List<Items>();

  itemList.Add(new Items { Year = 22, Course = "B.SC", Name = "Jegadees" });
  itemList.Add(new Items { Year = 23, Course = "M.SC", Name = "Arun" });
  itemList.Add(new Items { Year = 55, Course = "B.Tech", Name = "Kanaga" });

  DataGrid.ItemsSource = itemList;

项目集合 -

 public class Items
    {        
        public string Name { get; set; }
        public string Course { get; set; }
        public int Year { get; set; }
    }

推荐答案

具有Item的DataContext的祖先是DataGridRow的对应DataGridRowHeader

The ancestor with a DataContext of Item is the DataGridRow of the corosponding DataGridRowHeader

 <TextBlock Text="{Binding Path=DataContext.Year, 
                           RelativeSource={RelativeSource Mode=FindAncestor, 
                                            AncestorType=DataGridRow}}" /> 

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

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