DataGrid row values position changing on mouse click
I h开发者_如何学Pythonave a very strange issue with WPF datagrid row mouse click. When ever I click on a row that row's data moves to top of the row. But when it loads the first time all row data is center-aligned.
Am I missing any property in the datagrid?
WPF's DataGrid uses a TextBlock for the cell when the row is not in edit mode. When it is in edit mode, a TextBox is used. Neither control has a property that affects the vertical alignment within the control, but using a style you could set the VerticalAlignment of the TextBox itself to be center-aligned. This might cause undesired side effects though since there will now be areas of the edited cell not covered by the TextBox.
<DataGridTextColumn ...>
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="{x:Type TextBox}">
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
</DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>
Apply the following CellStyle to you DataGrid, and increase the RowHeight temporarily to verify you get the behaviour you expect. The following cell style aligns horizontally left and vertically in the center.
<Window.Resources>
<Style x:Key="LeftAlignedCellStyle" TargetType="{x:Type WpfToolkit:DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type WpfToolkit:DataGridCell}">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Left" VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<WpfToolkit:DataGrid
RowHeight="40"
CellStyle="{StaticResource LeftAlignedCellStyle}"
ItemsSource="{Binding Path=GridData, Mode=OneWay}">
</WpfToolkit:DataGrid>
精彩评论