WPF DataGrid RowHeader数据绑定 [英] WPF DataGrid RowHeader databinding
问题描述
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屋!