Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: XAML WPF C#4.0 DataGrid
please anybody give me the C# equivalent of the below code
 
 
 <dg:DataGridTextColumn.ElementStyle>
                        <Style TargetType="TextBlock">
                            <Setter Property="TextAlignment" Value="Center" />
                        </Style>
                    </dg:DataGridTextColumn.ElementStyle>
 

thanks in advance
Posted 20-Jan-12 10:02am
Comments
BillWoodruff at 20-Jan-12 19:27pm
   
You mean the C# code in WPF that will replicate the function and behavior of that XAML ?
MohammadIqbal at 20-Jan-12 22:39pm
   
Yes exactly i want that
Amir Mahfoozi at 21-Jan-12 0:23am
   
You can save that style as a resource and give it a name then set a given element style by CSharp code.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hello
 
Style style = new System.Windows.Style();
style.TargetType = typeof(TextBlock);
 
Setter setter = new Setter();
setter.Property = DependencyProperty.Register("TextAlignment", typeof(TextAlignment), typeof(Setter));
setter.Value = TextAlignment.Center;
style.Setters.Add(setter);
DataGridTextColumn dg = new DataGridTextColumn();
dg.ElementStyle = style;
 
I think this can help.
  Permalink  
Comments
MohammadIqbal at 23-Jan-12 5:16am
   
in fact i have a DataGrid Named datagrid1 in this context how do I link this "style" specifically to datagrid1 . I tried like this datagrid1.HeaderStyle=style but it is not working . the same approach for changing the style of the Header worked. but textalignment changing is not taking place
Shahin Khorshidnia at 23-Jan-12 16:27pm
   
Please write the whole of the code here.
MohammadIqbal at 24-Jan-12 13:56pm
   
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;
MohammadIqbal at 24-Jan-12 13:55pm
   
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 content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 565
1 Maciej Los 319
2 Richard MacCutchan 220
3 BillWoodruff 164
4 Mathew Soji 160
0 OriginalGriff 8,654
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,229
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 21 Jan 2012
Copyright © CodeProject, 1999-2014
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