tags:

views:

79

answers:

2

Hi,

I have a very strange issue with wpf datagrid row mouse click.when ever i click on a row that row's data moving to top of the row. But when it loads first time all rows data is center aligned.

Am i missing any property on datagrid.

Please do the needful .

Thanks In Advance.

regards KSR

A: 

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>
Josh Einstein
Hi Josh , thanks for your quick reply. but in my case there is no editing happening. Actual requiremet is when i double click a row with mouse, on first click it self row data is moving up. it is causing problem in UI look.please guide me in this issue
srinivas
Can you reproduce it in a short example that you can post?
Josh Einstein
A: 

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>
Zamboni
Thanks a lot Zamboni. This solution resolved my issue.
srinivas