0

Contents of the Datagrid cell need to centered. Set the textalignment property. But the contents are not centered. Am i missing something.

        Style aStyle = new Style()
        {
          TargetType = typeof(TextBlock)

         };

           aStyle.Setters.Add(new Setter(TextBlock.TextAlignmentProperty,TextAlignment.Center));

        DataGridTextColumn c1 = new DataGridTextColumn();
        c1.Width = 20;
        c1.Header = "Index";
        c1.Binding = new Binding("Index");
        c1.Width = 110;
        c1.IsReadOnly = true;
        c1.ElementStyle = aStyle;
        aDataGrid.Columns.Add(c1);
Unknown
  • 306
  • 1
  • 3
  • 16
  • This works for me. If I add a line aDataGrid.Items.Add(new { Index="Blah" });, I can see that "Blah" is horizontally centered in the index column. You're not trying to center the column header itself are you? – Rowbear Nov 02 '15 at 17:37
  • This question was already answered in this thread [here](http://stackoverflow.com/questions/20352231/create-style-for-textblock-in-datagridtextcolumn) – Chris34800 Nov 03 '15 at 08:45

0 Answers0