minecraftplayer1234
minecraftplayer1234

Reputation: 2227

DataGrid different first column

I have a WPF app. I use Mahapps.Metro. The main control on the main window is a DataGrid. I wanted it to be a table, but I wanted column headers and first cell in every row to have a slightly different style. For now it looks like this:

enter image description here

So column headers are fine. But now I want the first cell in every row to be special, like column headers (bigger font, bold, maybe a vertical border line, like the horizontal one). Is there a way to do it? (remembering the fact, that I add every column and row in code, not in XAML).

For now my XAML code looks like this:

<Controls:MetroWindow x:Class="StateMachines.MainWindow"
                      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                      xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
                      Title="Maszyny Mealy'ego i Moora"
                      Height="450"
                      Width="800">
    <Controls:MetroWindow.RightWindowCommands>
        <Controls:WindowCommands>
            <Button Content="settings" />
            <Button>
                <StackPanel Orientation="Horizontal">
                    <Rectangle Width="20"
                               Height="20"
                               Fill="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=Foreground}">
                        <Rectangle.OpacityMask>
                            <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_cupcake}" />
                        </Rectangle.OpacityMask>
                    </Rectangle>
                    <TextBlock Margin="4 0 0 0"
                               VerticalAlignment="Center"
                               Text="do something" />
                </StackPanel>
            </Button>
        </Controls:WindowCommands>
    </Controls:MetroWindow.RightWindowCommands>
    <DataGrid x:Name="DataGridLogic" Margin="318,59,0,136" Width="452" RenderTransformOrigin="0.657,1.249"
              ItemsSource="{Binding Path=Logic.DefaultView}" ColumnWidth="*" CanUserReorderColumns="False"
              CanUserResizeColumns="False" CanUserAddRows="False"  CanUserSortColumns="False" CanUserResizeRows="False">
        <DataGrid.ColumnHeaderStyle>
            <Style TargetType="{x:Type DataGridColumnHeader}" BasedOn="{StaticResource MetroDataGridColumnHeader}">
                <Setter Property="HorizontalContentAlignment" Value="Center" />
            </Style>
        </DataGrid.ColumnHeaderStyle>
        <DataGrid.RowHeaderStyle>
            <Style TargetType="{x:Type DataGridRowHeader}" BasedOn="{StaticResource MetroDataGridRowHeader}">
                <Setter Property="HorizontalContentAlignment" Value="Center" />
            </Style>
        </DataGrid.RowHeaderStyle>
        <DataGrid.CellStyle>
            <Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource MetroDataGridCell}">
                <Setter Property="HorizontalContentAlignment" Value="Center" />
            </Style>
        </DataGrid.CellStyle>
    </DataGrid>
</Controls:MetroWindow>

Upvotes: 0

Views: 369

Answers (1)

Neil B
Neil B

Reputation: 2224

I would use a style trigger based on the displayindex.

<Style.Triggers>
    <DataTrigger Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Column.DisplayIndex}" Value="0">
        <Setter Property="Block.FontWeight" Value="Bold"/>
    </DataTrigger>
</Style.Triggers>

Upvotes: 1

Related Questions