0

I'm trying to center an image inside a DataGrid Cell. I've tried all the current code at the same time and each "centering" solutions one at a time and still get the image centered in left.

<DataGrid ItemsSource="{Binding PhotoList, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"
          Grid.Row="1"
          x:Name="dataGridPhotos"
          Margin="5 5 5 5"
          CanUserAddRows="False"
          CanUserDeleteRows="True"
          AutoGenerateColumns="False"
          HorizontalContentAlignment="Center">

    <DataGrid.Columns>

        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Image Source="{Binding PhotoSource}" 
                           Width="320px" 
                           Height="240px" 
                           Stretch="Uniform"
                           Margin="0"
                           HorizontalAlignment="Center"/>
                </DataTemplate>

            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>


    </DataGrid.Columns>

    <DataGrid.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="HorizontalAlignment" Value="Center"/>
        </Style>
    </DataGrid.CellStyle>
</DataGrid>
dymanoid
  • 14,771
  • 4
  • 36
  • 64
rgomez
  • 185
  • 1
  • 2
  • 17
  • Have you tried HorizontalContentAlignment? – Clemens Jun 04 '18 at 14:27
  • You should set `HorizontalContentAlignment` to Strech on `DataGrid` and set the `Image` 's `HorizontalAlignment` to Center. – user1672994 Jun 04 '18 at 14:29
  • Setting the HorizontalAlignment on the Image element alone should work. You don't need the CellStyle nor setting the HorizontalContentAlignment property of the DataGrid. How does the column look like? – mm8 Jun 04 '18 at 14:30
  • None of the suggestions above worked. – rgomez Jun 04 '18 at 14:32
  • You mean create `Grid` and set it's `HorizontalAlignment` to `Center`? Placing image inside Grid ofc. – rgomez Jun 04 '18 at 14:45
  • 2
    It works just fine for me. This is one of the reasons why you should always include a Minimal, Complete, and Verifiable example when asking a question on SO: https://stackoverflow.com/help/mcve – mm8 Jun 04 '18 at 15:05
  • 1
    Possible duplicate of [How to center the content of cells in a data grid?](https://stackoverflow.com/questions/6072093/how-to-center-the-content-of-cells-in-a-data-grid) – Chris W. Jun 04 '18 at 21:44

0 Answers0