Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
858 views
in Technique[技术] by (71.8m points)

data binding - WPF DataGrid RowHeader databinding

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...