简体   繁体   English

绑定在WPF Datagrid第二行标题中不起作用

[英]Binding not working in WPF Datagrid second row header

My Xaml- 我的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>

in C# 在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;

Item Collection - 物品收集-

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

在此处输入图片说明

The ancestor with a DataContext of Item is the DataGridRow of the corosponding DataGridRowHeader 具有Item的DataContext的祖先是对应的DataGridRowHeader的DataGridRow

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

With this piece of code RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridRowHeader}}" , you are travelling upto Visual Tree till you find first DataGridRowHeader . 借助这段代码RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridRowHeader}}" ,您将进入Visual Tree,直到找到第一个DataGridRowHeader为止。

Issue is in your Path - Path=Items.Year . 问题出在您的Path- Path=Items.Year

It will search for property Items in DataGridRowHeader which obviously it doesn't have. 它将在DataGridRowHeader中搜索显然没有的属性Items

You need to change Path to like this DataContext.Year because DataContext will get you actual object it's binded to ie an instance of Items class and then bind to its property Year in that instance. 您需要将Path更改为类似DataContext.Year因为DataContext将为您提供绑定到Item 实例的实际对象,然后再绑定到该实例的属性Year

So correct binding would be like this: 因此正确的绑定将是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM