当我尝试将数据视图绑定到数据网格并尝试让它将第一行显示为行标题时,我遇到了问题。
我的数据网格如下所示:
<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>
此代码发生的情况是,数据网格插入一个行头,然后将数据视图中的数据放在网格的其余部分,行头留空。将数据视图的第一列获取到行标题的好解决方案是什么?
我最终用一种风格解决了这个问题,如下所示:
<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>