dimanche 10 mai 2015

WPF datagrid text alignment

I have a small but annoying problem with my datagrid.

I have a datagrid where the Title column has a left text alignment, the other columns have a center alignment. When the user edits the Title then the text moves to the center (seems like the textbox is causing this). When the user presses enter or clicks the row he/she is editing, then the text stays in the center position. If the user clicks an other row then the text correctly moves back to it's left position.

I would like to see that the Title is always aligned to the left instead of sometimes staying in the middle. I have tried many things and searched a lot but no luck so far. (the application is written in MVVM)

This is the style code of the datagrid:

<DataGrid.CellStyle>
                    <Style TargetType="{x:Type DataGridCell}">
                        <Setter Property="BorderThickness" Value="0"/>
                        <Setter Property="Height" Value="35"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type DataGridCell}">
                                    <Grid>
                                        <ContentPresenter HorizontalAlignment="Center" />
                                        <ContentPresenter VerticalAlignment="Center" />
                                    </Grid>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                        <Style.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background" Value="{StaticResource CustomTextDark}"/>
                            </Trigger>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="Background" Value="#FFA1A1A1"/>
                                <Setter Property="Foreground" Value="{StaticResource CustomTextDark}"/>
                                <Setter Property="FontWeight" Value="Bold"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </DataGrid.CellStyle>
                <DataGrid.RowStyle>
                    <Style TargetType="{x:Type DataGridRow}">
                        <Setter Property="Background" Value="#FFDDDDDD"/>
                        <Setter Property="Foreground" Value="#FF3E3E42"/>
                        <Setter Property="FontSize" Value="15"/>
                        <Style.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background" Value="#FFA1A1A1"/>
                                <Setter Property="Foreground" Value="{StaticResource CustomTextDark}"/>
                            </Trigger>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="Background" Value="#FFA1A1A1"/>
                                <Setter Property="Foreground" Value="Black"/>
                                <Setter Property="BorderThickness" Value="0"/>
                                <Setter Property="FontWeight" Value="Bold"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </DataGrid.RowStyle>

Aucun commentaire:

Enregistrer un commentaire