WPF DataGrid RowHeader 数据绑定 [英] WPF DataGrid RowHeader databinding
问题描述
我有一个 DataGrid,绑定到一个 DataTable.我想在 RowHeader 中显示文本,以实现这样的目标:
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.
这对我有用:
<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
属性绑定到您关心的它的 Item 属性,在本例中为 Header
(用 XAML 说可能比用英语说得更容易).
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).
然后在 .xaml.cs 中:
Then in the .xaml.cs:
ui_dataGrid.ItemsSource = dataSource.Rows;
注意每个 Row
对象都有一个 Header
属性,这也是我要绑定的.
N.B. Each Row
object has a Header
property which is what I'm binding too.
这篇关于WPF DataGrid RowHeader 数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!