绑定到dataview时的WPF datagrid rowheader

时间:2012-05-09 10:43:01

标签: wpf datagrid styles

当我尝试将数据视图绑定到数据网格并尝试将其显示为第一行作为行标题时,我遇到了问题。

我的datagrid看起来像这样:

<DataGrid DockPanel.Dock="Top" Margin="15,15" HorizontalAlignment="Left" Name="ReportDataGrid" CanUserSortColumns="False"  CanUserResizeColumns="False" CanUserReorderColumns="False" AutoGenerateColumns="True" CanUserResizeRows="False" IsReadOnly="True" HorizontalScrollBarVisibility="Auto" HeadersVisibility="All" Visibility="{Binding Path=ShowReportDataGrid}" SelectionMode="Single" SelectionUnit="Cell" ClipboardCopyMode="IncludeHeader">
    <DataGrid.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="Width" Value="48"></Setter>
                </Style>
                    </DataGrid.CellStyle>
                        <DataGrid.ColumnHeaderStyle>
                            <Style TargetType="DataGridColumnHeader">
                                <Setter Property="Background" Value="{StaticResource DataGrid_Style_Header}" />
                                <Setter Property="Foreground" Value="White" />
                                <Setter Property="Padding" Value="10,0,10,0"/>
                                <Setter Property="Width" Value="48"></Setter>
                            </Style>
                        </DataGrid.ColumnHeaderStyle>
                        <DataGrid.RowHeaderStyle>
                            <Style TargetType="DataGridRowHeader">
                                <Setter Property="Background" Value="{StaticResource DataGrid_Style_Header}" />
                                <Setter Property="Foreground" Value="White" />
                                <Setter Property="Padding" Value="10,0,10,0"/>
                                <Setter Property="Width" Value="48"></Setter>
                            </Style>
                        </DataGrid.RowHeaderStyle>
                    </DataGrid>

此代码的作用是数据网格插入一个行标题,然后将数据视图中的数据放入网格的其余部分,行标题为空。将数据视图的第一列放到rowheader会有什么好处?

1 个答案:

答案 0 :(得分:0)

我最终用风格来解决问题,如下所示:

<DataGrid.CellStyle>
    <Style TargetType="DataGridCell">
        <Setter Property="Width" Value="48"></Setter>
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Column.DisplayIndex, Mode=OneWay}" Value="0">
                    <Setter Property="Background" Value="{StaticResource DataGrid_Style_Header}" />        
                    <Setter Property="Foreground" Value="White" />
                    <Setter Property="Width" Value="60"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.CellStyle>