WPF DataGrid RowHeader数据绑定 [英] WPF DataGrid RowHeader databinding

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

问题描述

我有一个DataGrid绑定到一个DataTable。我想在RowHeader中显示文本,实现如下所示:

  Col0 Col1 Col2 Col3 
表| 1 | 3 | 5 | 6 |
椅子| 3 | 2 | 1 | 8 |

这是可能吗?如果是这样,我该怎么做?

解决方案

我尝试了两个答案,对我而言都没有。基本上我要做的就是将它们组合在一起。



这对我有用:

 code>< DataGrid name =ui_dataGrid> 
< DataGrid.RowHeaderTemplate>
< DataTemplate>
< TextBlock Text ={Binding RelativeSource = {RelativeSource Mode = FindAncestor ,
AncestorType = {x:Type DataGridRow}},
Path = Item.Header}/>
< / DataTemplate>
< /DataGrid.RowHeaderTemplate>
< / DataGrid>

诀窍是找到祖先 DataGridRow ,然后将 TextBlock.Text 属性绑定到您所关心的项目的属性,在这种情况下标题(XAML比英文更容易)。



然后在.xaml.cs中:

  ui_dataGrid.ItemsSource = dataSource.Rows; 

NB每个对象有一个标头属性,这也是我绑定的。


I have a DataGrid, bound to a DataTable. I want to display text in the RowHeader, to achieve something like this:

         Col0      Col1      Col2      Col3
Table |    1    |    3    |    5    |    6    |
Chair |    3    |    2    |    1    |    8    |

Is this possible and if so, how can I do this?

解决方案

I tried both answers, and neither worked for me. Essentially what I had to do was mix them together.

This works for me:

<DataGrid name="ui_dataGrid>
    <DataGrid.RowHeaderTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
                                      AncestorType={x:Type DataGridRow}}, 
                                      Path=Item.Header}"/>
        </DataTemplate>
    </DataGrid.RowHeaderTemplate>
</DataGrid>

The trick is to find the ancestor DataGridRow, then Bind the TextBlock.Text attribute to its Item's property that you care about, in this case Header (easier said in XAML than English maybe).

Then in the .xaml.cs:

ui_dataGrid.ItemsSource = dataSource.Rows;

N.B. Each Row object has a Header property which is what I'm binding too.

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

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