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

问题描述

我有一个已填充的DataTable(xDataTable),其中包含7个已定义的列-我想用作RowHeader的第一列。

I have a populated DataTable (xDataTable) with 7 defined columns - the first column I want to use as my RowHeader.

我也有一个DataGrid:

I also have a DataGrid:

 <DataGrid x:Name="DataGridX" ItemsSource="{Binding}" Grid.Row="0"
           CanUserAddRows="False" SelectionUnit="Cell" />

然后我设置DataGrid的DataContext:

I then set the DataContext of my DataGrid:

DataGridX.DataContext = xDataTable;

这全部有效-但是如何将DataGrid的第一列设置为RowHeader?

This all works - but how can I set the first column of my DataGrid as a RowHeader?

推荐答案

使用以下样式(通常情况):

<DataGrid.RowHeaderStyle>
    <Style TargetType="{x:Type DataGridRowHeader}">
        <Setter Property="Content" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},Path=Columns[0].Header,Mode=OneTime}" />
    </Style>
</DataGrid.RowHeaderStyle>

如果我们要为单独的Row使用单独的RowHeader,则使用:

<DataGrid.RowStyle>
    <Style TargetType="{x:Type DataGridRow}">
        <Setter Property="IsSelected" Value="{Binding IsRowSelected}" />
        <Setter Property="Header" Value="{Binding Content}" />
    </Style>
</DataGrid.RowStyle>

只需根据需要更改上述绑定。

Just change the above binding as required.

如果第一列是:

<DataGridTextColumn Binding="{Binding Content}" Header="Content"/>

然后删除此列并将此绑定用于页眉。

then remove this column and use this binding for the Header.

这篇关于DataGrid行标题WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 04:50