WPF DataGrid 表头单元格居中DataGridCheckBoxColumn居中单击选中

以下代码实现了DataGrid的一些简单样式功能:

1、表头居中,表头填充满整个DataGrid

2、单元格值居中

3、CheckBox居中,单击选中功能

 

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Items}"  CanUserAddRows="False" CanUserResizeColumns="False" CanUserReorderColumns="False" CanUserSortColumns="False" ColumnWidth="*">
            <DataGrid.Columns>
                <DataGridCheckBoxColumn Header="" Binding="{Binding IsSelected,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="auto">
                    <DataGridCheckBoxColumn.ElementStyle>
                        <Style TargetType="CheckBox">
                            <Setter Property="HorizontalAlignment" Value="Center"></Setter>
                            <Setter Property="VerticalAlignment" Value="Center"></Setter>
                            <Setter Property="Margin" Value="3"></Setter>
                        </Style>
                    </DataGridCheckBoxColumn.ElementStyle>
                </DataGridCheckBoxColumn>
                <DataGridTemplateColumn Header="加分项" IsReadOnly="True" Width="*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock TextWrapping="Wrap" Text="{Binding Name}" ToolTip="{Binding Name}" Margin="3"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="分值" IsReadOnly="True" MinWidth="100" Width="auto" Binding="{Binding Score}">
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="TextBlock">
                            <Setter Property="HorizontalAlignment" Value="Center"></Setter>
                            <Setter Property="VerticalAlignment" Value="Center"></Setter>
                            <Setter Property="Margin" Value="3"></Setter>
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>
                <DataGridTextColumn Header="备注" IsReadOnly="True" MinWidth="100" Width="auto" Binding="{Binding Note}">
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="TextBlock">
                            <Setter Property="HorizontalAlignment" Value="Center"></Setter>
                            <Setter Property="VerticalAlignment" Value="Center"></Setter>
                            <Setter Property="Margin" Value="3"></Setter>
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>
            </DataGrid.Columns>
            <DataGrid.ColumnHeaderStyle>
                <Style TargetType="DataGridColumnHeader">
                    <Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
                </Style>
            </DataGrid.ColumnHeaderStyle>
        </DataGrid>

 

posted on 2021-04-06 15:32  梦琪小生  阅读(1345)  评论(0编辑  收藏  举报

导航