WPF - C# Desktop App - DateGrid - TextAlignment Center for the cells

Markus Freitag 3,791 Reputation points
2020-07-25T19:32:32.653+00:00

Hello,

<DataGrid Grid.Column="1"    
              ItemsSource="{Binding View2}"   
              IsReadOnly="True"     
              AutoGenerateColumns="False" Margin="54,669,429,22" FontSize="18">  
            <DataGrid.Columns>  
                <DataGridTextColumn Header="Slot Nummer" Binding="{Binding SlotNo}"/>  
                <DataGridTextColumn Header="Materialnummer" Binding="{Binding Number}"/>  

13713-1.jpg

How do I get cells as text alignment in the middle.
And how can I make the active line green from the code behind?

Thanks for answers in advance.
Best regards Markus

Developer technologies | Windows Presentation Foundation
0 comments No comments
{count} votes

Answer accepted by question author
  1. Peter Fleischer (former MVP) 19,341 Reputation points
    2020-07-27T04:06:52.12+00:00

    Hi Markus,
    You can set the style for DataGridColumnHeader and DataGridCell without Key. The style then affects all cells.
    13844-x.png

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,341 Reputation points
    2020-07-26T04:51:31.453+00:00

    Hi Markus,
    You can set the style for DataGridCell without Key. The style then affects all cells.

    13714-x.png

    1 person found this answer helpful.

  2. DaisyTian-1203 11,651 Reputation points Moderator
    2020-07-27T03:21:26.18+00:00

    1.Set SelectionUnit="FullRow" for your DataGrid to make it selected by row.

    2.Use ElementStyle="{StaticResource mytext}" for the DataGridTextColumn to make the active text to Green in the code:

    In the cs code add : this.dataGrid.Resources["columnForeground"] = Brushes.Green;

    Here is the xaml code:
    13795-20-07-27-112018.jpg

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.