Click here to Skip to main content
11,707,470 members (74,908 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF C#4.0 DataGrid
private void dataGrid1_AutoGeneratedColumns(object sender, EventArgs e)
      {
 

          Style mystyle = new System.Windows.Style();
          Setter set = new Setter();
          set.Property = DependencyProperty.Register("TextAlignment", typeof(TextAlignment),
              typeof(Setter));
          mystyle.TargetType = typeof(TextBlock);
          set.Value = TextAlignment.Right;
          mystyle.Setters.Add(set);
 
          dataGrid1.AutoGenerateColumns = true;
 

           DataGridColumnHeader header =new DataGridColumnHeader();
           Style headerstyle = new System.Windows.Style();
          headerstyle.TargetType = header.GetType();
          Setter setter = new Setter();
          Setter setter1 = new Setter();
          Setter setter2 = new Setter();
          setter.Property = FontSizeProperty;
          FontSize = 15;
          setter.Value = FontSize;
          headerstyle.Setters.Add(setter);
          setter1.Property = ForegroundProperty;
          Foreground = Brushes.BlueViolet;
          setter1.Value = Foreground;
          headerstyle.Setters.Add(setter1);
          Setter setter3 = new Setter();
          LinearGradientBrush lb = new LinearGradientBrush();
          Point startpoint=new Point(0.5,0);
          Point endpoint = new Point(1, 1);
          lb.StartPoint = startpoint;
          lb.EndPoint = endpoint;
          GradientStop gst = new GradientStop();
          lb.GradientStops.Add(new GradientStop(Colors.Yellow, 0.0));
          lb.GradientStops.Add(new GradientStop(Colors.Orange, 0.5));
          lb.GradientStops.Add(new GradientStop(Colors.Red, 1.0));
          setter3.Property = BackgroundProperty;
          setter3.Value = lb;
          headerstyle.Setters.Add(setter3);
          headerstyle.Setters.Add(set);
        dataGrid1.HeaderStyle = headerstyle;

this code yield datagrid1 Headerstyle change . but the text alignment in the column can't be set it always lie on the left
what I have to do for this

any help is highly appreaciated
Posted 23-Jan-12 3:34am
Edited 24-Jan-12 3:06am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Mika Wendelius 220
1 Maciej Los 195
2 OriginalGriff 148
3 Sergey Alexandrovich Kryukov 145
4 chainerlt 110
0 OriginalGriff 9,198
1 Sergey Alexandrovich Kryukov 8,562
2 CPallini 5,189
3 Maciej Los 4,921
4 Mika Wendelius 3,856


Advertise | Privacy | Mobile
Web04 | 2.8.150819.1 | Last Updated 24 Jan 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100