Israel Lopez
Israel Lopez

Reputation: 1145

Apply Styles to DataGridTextColumn with MahApps Metro

I just tried using the following XAML in my WPF application.

<DataGridTextColumn Header="Due" Binding="{Binding QTYDue, Mode=OneWay}">
    <DataGridTextColumn.CellStyle>
        <Style>
            <Setter Property="FrameworkElement.HorizontalAlignment" Value="Center" />
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

Unfortunately, the behavior is that the previous styling for the control in MahApps Metro is removed. How do I keep the existing style, but just modify this one aspect of it.

WPF Behavior

Upvotes: 1

Views: 964

Answers (1)

jsanalytics
jsanalytics

Reputation: 13188

Try this:

Style:

<Style x:Key="DatagridCellStyle1" 
       TargetType="{x:Type DataGridCell}" 
       BasedOn="{StaticResource {x:Type DataGridCell}}">
    <Setter Property="HorizontalAlignment" Value="Center" />
</Style>

XAML:

        <DataGridTextColumn Header="DUE" 
                            Binding="{Binding QTYDue}" 
                            CellStyle="{StaticResource DatagridCellStyle1}" />

OR

            <DataGridTemplateColumn Header="DUE">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <DataGridCell Style="{StaticResource DatagridCellStyle1}">
                            <TextBlock Text="{Binding QTYDue}"></TextBlock>
                        </DataGridCell>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

enter image description here

Upvotes: 3

Related Questions