프로그래밍/WPF

[WPF] DataGrid Style 설정

흔한티벳여우 2021. 9. 1. 10:28
반응형

스타일 설정할때 필요한 몇가지 적어논다. 필요하신 분들은 저런 방식으로 적용 가능함

DataGrid Background 처리할때 등등으로 유용합니다.

<DataGrid Grid.Row="1" x:Name="dgCommand" AutoGenerateColumns="False" IsReadOnly="True"
                  HorizontalGridLinesBrush="Gray"
                  VerticalGridLinesBrush="Gray">
            <DataGrid.Resources>
                <Style TargetType="{x:Type DataGrid}">
                    <Setter Property="Background" Value="#2d2d30"/>
                </Style>
                <Style TargetType="{x:Type DataGridRow}">
                    <Setter Property="Background" Value="#2d2d30"/>
                </Style>
                <Style TargetType="{x:Type DataGridRowHeader}">
                    <Setter Property="Background" Value="#2d2d30"/>
                    <Setter Property="FontWeight" Value="Bold"/>
                </Style>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="Background" Value="#2d2d30"/>
                    <Setter Property="Foreground" Value="White"/>
                    <Setter Property="Padding" Value="5 0 5 0"/>
                    <Setter Property="FontWeight" Value="Bold"/>
                    <Setter Property="FontSize" Value="15"/>
                    <Setter Property="HorizontalAlignment" Value="Center"/>
                </Style>
                <Style TargetType="{x:Type Button}" x:Key="{ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}">
                    <Setter Property="Background" Value="Black" />
                </Style>

                <Style x:Key="Content" TargetType="{x:Type TextBlock}">
                    <Setter Property="Foreground" Value="White"/>
                    <Setter Property="HorizontalAlignment" Value="Center"/>
                </Style>
            </DataGrid.Resources>
            <DataGrid.Columns>
                <DataGridTextColumn Header="Index" Binding="{Binding Index}" 
                                    ElementStyle="{StaticResource Content}"
                                    Width="Auto"/>
                <DataGridTextColumn Header="Type" Binding="{Binding CommandType}"
                                    ElementStyle="{StaticResource Content}"
                                    Width="Auto"/>
                <DataGridTextColumn Header="Storage" Binding="{Binding StorageIndex}"
                                    ElementStyle="{StaticResource Content}"
                                    Width="Auto"/>
                <DataGridTextColumn Header="Floor" Binding="{Binding FloorIndex}"
                                    ElementStyle="{StaticResource Content}"
                                    Width="Auto"/>
                <DataGridTextColumn Header="Bay" Binding="{Binding BayIndex}"
                                    ElementStyle="{StaticResource Content}"
                                    Width="Auto"/>
                <DataGridTextColumn Header="RealWorldTime" Binding="{Binding RealWorldTime}"
                                    ElementStyle="{StaticResource Content}"
                                    Width="*"/>
                <DataGridTextColumn Header="SimulateTime" Binding="{Binding SimulateTime}"
                                    ElementStyle="{StaticResource Content}"
                                    Width="*"/>
            </DataGrid.Columns>
        </DataGrid>

 

반응형