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
 
  <dg:DataGrid.Columns>
                <dg:DataGridTextColumn Header="AuthorID" FontWeight="Black" Foreground="Red"
                                       FontSize="16" FontStyle="Italic" 
                                       CanUserReorder="True"  
                                       CanUserSort="True" SortDirection="Ascending">
                    <dg:DataGridTextColumn.ElementStyle>
                        <Style TargetType="TextBlock">
                            <Setter Property="TextAlignment" Value="Center" />
                        </Style>
                    </dg:DataGridTextColumn.ElementStyle>
                    <dg:DataGridTextColumn.EditingElementStyle>
                        <Style TargetType="TextBox">
                            <Setter Property="TextWrapping" Value="Wrap" />
                            <Setter Property="AcceptsReturn" Value="true" />
                        </Style>
                    </dg:DataGridTextColumn.EditingElementStyle>
                    <dg:DataGridColumn.HeaderStyle>
                        <Style TargetType="{x:Type dg:DataGridColumnHeader}">
                            <Setter Property="Background" >
                                <Setter.Value>
                                    <LinearGradientBrush EndPoint="1,1" StartPoint="0.5,0" SpreadMethod="Pad">
                                        <GradientStop Color="Bisque" Offset="0" />
                                        <GradientStop Color="Cyan" Offset="1" />
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="Foreground" Value="Black" />
                            <Setter Property="FontSize" Value="20" />
                            <Setter Property="HorizontalContentAlignment" Value="Center" />
                            <Setter Property="BorderBrush" Value="yellow"/>
                            <Setter Property="BorderThickness" Value="2,2,2,2"/>
                        </Style>
                    </dg:DataGridColumn.HeaderStyle>
                </dg:DataGridTextColumn>
                <dg:DataGridTextColumn Header="FirstName" FontWeight="Bold" Foreground="DarkGreen" FontSize="16" FontStyle="Italic" 
                                       CanUserSort="False" Binding="{Binding Path=FirstName}">
                    <dg:DataGridTextColumn.ElementStyle>
                        <Style TargetType="TextBlock">
                            <Setter Property="TextAlignment" Value="Center" />
                        </Style>
                    </dg:DataGridTextColumn.ElementStyle>
                    <dg:DataGridTextColumn.EditingElementStyle>
                        <Style TargetType="TextBox">
                            <Setter Property="TextWrapping" Value="Wrap" />
                            <Setter Property="AcceptsReturn" Value="true" />
                        </Style>
                    </dg:DataGridTextColumn.EditingElementStyle>
                    <dg:DataGridColumn.HeaderStyle>
                        <Style TargetType="{x:Type dg:DataGridColumnHeader}">
 
                            <Setter Property="Background" >
                                <Setter.Value>
                                    <LinearGradientBrush EndPoint="1,1" StartPoint="0.5,0" SpreadMethod="Pad">
                                        <GradientStop Color="Lavender" Offset="0" />
                                        <GradientStop Color="BlanchedAlmond" Offset="1" />
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="Foreground" Value="Black" />
                            <Setter Property="FontSize" Value="20" />
                            <Setter Property="HorizontalContentAlignment" Value="Center" />
                            <Setter Property="Width" Value="Auto" />
                            <Setter Property="BorderBrush" Value="Yellow"/>
                            <Setter Property="BorderThickness" Value="2,2,2,2"/>
                        </Style>
                    </dg:DataGridColumn.HeaderStyle>
                </dg:DataGridTextColumn>
                <dg:DataGridTextColumn Header="LastName" FontWeight="Bold" Foreground="OrangeRed" FontSize="16" FontStyle="Italic" 
                                       CanUserSort="False" Binding="{Binding Path=LastName}">
                    <dg:DataGridTextColumn.ElementStyle>
                        <Style TargetType="TextBlock">
                            <Setter Property="TextAlignment" Value="Center" />
                        </Style>
                    </dg:DataGridTextColumn.ElementStyle>
                    <dg:DataGridTextColumn.EditingElementStyle>
                        <Style TargetType="TextBox">
                            <Setter Property="TextWrapping" Value="Wrap" />
                            <Setter Property="AcceptsReturn" Value="true" />
                        </Style>
                    </dg:DataGridTextColumn.EditingElementStyle>
                    <dg:DataGridColumn.HeaderStyle>
                        <Style TargetType="{x:Type dg:DataGridColumnHeader}">
                            <Setter Property="Background" >
                                <Setter.Value>
                                    <LinearGradientBrush EndPoint="1,1" StartPoint="0.5,0" SpreadMethod="Pad">
                                        <GradientStop Color="Brown" Offset="0" />
                                        <GradientStop Color="White" Offset="1" />
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="Foreground" Value="Black" />
                            <Setter Property="FontSize" Value="20" />
                            <Setter Property="HorizontalContentAlignment" Value="Center" />
                            <Setter Property="Width" Value="Auto" />
                            <Setter Property="BorderBrush" Value="Yellow"/>
                            <Setter Property="BorderThickness" Value="2,2,2,2"/>
                        </Style>
                    </dg:DataGridColumn.HeaderStyle>
                </dg:DataGridTextColumn>
                <dg:DataGridTemplateColumn Header="DOB" MinWidth="100" CanUserSort="False">
                    <dg:DataGridTemplateColumn.HeaderStyle>
                        <Style TargetType="{x:Type dg:DataGridColumnHeader}">
                            <Setter Property="Background" >
                                <Setter.Value>
                                    <LinearGradientBrush EndPoint="1,.1" StartPoint="0.5,1" SpreadMethod="Pad">
                                        <GradientStop Color="Orange" Offset="1" />
                                        <GradientStop Color="Thistle" Offset="0" />
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="Foreground" Value="Black" />
                            <Setter Property="FontSize" Value="20" />
                            <Setter Property="HorizontalContentAlignment" Value="Center" />
                            <Setter Property="Width" Value="Auto" />
                            <Setter Property="BorderBrush" Value="yellow"/>
                            <Setter Property="BorderThickness" Value="2,2,2,2"/>
                        </Style>
                    </dg:DataGridTemplateColumn.HeaderStyle>
                    <dg:DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <dg:DatePicker SelectedDate="{Binding DOB}" SelectedDateFormat="Short" CalendarStyle="DynamicResource CalenderControlTemplate" removed="Yellow" />
                        </DataTemplate>
                    </dg:DataGridTemplateColumn.CellEditingTemplate>
                    <dg:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding DOB, StringFormat=d}" Foreground="DarkTurquoise" HorizontalAlignment="Center" />
                        </DataTemplate>
                    </dg:DataGridTemplateColumn.CellTemplate>
                </dg:DataGridTemplateColumn>
                <dg:DataGridCheckBoxColumn Header="Pass/Fail" Binding="{Binding Pass}">
                    <dg:DataGridColumn.HeaderStyle>
                        <Style TargetType="{x:Type dg:DataGridColumnHeader}">
                            <Setter Property="Background" >
                                <Setter.Value>
                                    <LinearGradientBrush EndPoint="1,1" StartPoint="0.5,0" SpreadMethod="Pad">
                                        <GradientStop Color="#FFC2D6F6" Offset="0" />
                                        <GradientStop Color="White" Offset="1" />
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="Foreground" Value="Black" />
                            <Setter Property="FontSize" Value="20" />
                            <Setter Property="HorizontalContentAlignment" Value="Center" />
                            <Setter Property="Width" Value="Auto" />
                            <Setter Property="BorderBrush" Value="Yellow"/>
                            <Setter Property="BorderThickness" Value="2,2,2,2"/>
                        </Style>
                    </dg:DataGridColumn.HeaderStyle>
                </dg:DataGridCheckBoxColumn>
                <dg:DataGridTextColumn Header="Nationality" FontWeight="Bold" Foreground="SandyBrown" 
                                       FontSize="16" FontStyle="Italic" CanUserSort="False" 
                                       Binding="{Binding Path= Nationality}" Width="Auto">
                    <dg:DataGridTextColumn.ElementStyle>
                        <Style TargetType="TextBlock">
                            <Setter Property="TextAlignment" Value="Center" />
                        </Style>
                    </dg:DataGridTextColumn.ElementStyle>
                    <dg:DataGridTextColumn.EditingElementStyle>
                        <Style TargetType="TextBox">
                            <Setter Property="TextWrapping" Value="Wrap" />
                            <Setter Property="AcceptsReturn" Value="true" />
                        </Style>
                    </dg:DataGridTextColumn.EditingElementStyle>
                    <dg:DataGridColumn.HeaderStyle>
                        <Style TargetType="{x:Type dg:DataGridColumnHeader}">
                            <Setter Property="Background" >
                                <Setter.Value>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0" SpreadMethod="Pad">
                                        <GradientStop Color="Blue" Offset="0" />
                                        <GradientStop Color="White" Offset="1" />
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="Foreground" Value="Black" />
                            <Setter Property="FontSize" Value="20" />
                            <Setter Property="HorizontalContentAlignment" Value="Center" />
                            <Setter Property="Width" Value="Auto" />
                            <Setter Property="BorderBrush" Value="Yellow"/>
                            <Setter Property="BorderThickness" Value="2,2,2,2"/>
                        </Style>
                    </dg:DataGridColumn.HeaderStyle>
                </dg:DataGridTextColumn>
            </dg:DataGrid.Columns>
 
 

can anybody help in C# code equivalent of this XAML
 
any type of help is appreciated
 
Iqbal
Posted 20-Jan-12 4:21am
Comments
SteveAdey at 20-Jan-12 9:26am
   
Why do you need the C# equivalent?
MohammadIqbal at 20-Jan-12 9:59am
   
I just to want to know how Column header will be set in DataGrid using C# code
SteveAdey at 20-Jan-12 10:43am
   
I have some code laying around somewhere that adds the columns in from code behind. I can dig it out at the weekend and post it up.
SAKryukov at 20-Jan-12 12:26pm
   
Of course this is possible to write, but do you expect complete code? This is a bit too much for Quick Questions & Answers forum.
--SA
MohammadIqbal at 20-Jan-12 12:32pm
   
i solved it myself

1 solution

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

Solution 1

The namespaces at the beginning of the XAML markup (which are not posted here) translate directly to CLR namespaces. The tags translate directly to classes from those namespaces and the attributes in the markup translate directly to properties of instances of those classes.
 
Just create instances of the classes mentioned in the markup, fill in the values from the XAML attributes into the corresponding properties and there you go.
  Permalink  
v2
Comments
MohammadIqbal at 20-Jan-12 12:06pm
   
can u just give an example i tried and not happening
SAKryukov at 20-Jan-12 12:28pm
   
If need to be more specific. What did you try and where did you fail? As I said in my comment to the question, the fool code would take a bit too much time...
--SA
SAKryukov at 20-Jan-12 12:27pm
   
Yes, this is exactly how to find out and implement appropriate calls in the code. My 5.
--SA
CDP1802 at 20-Jan-12 15:01pm
   
Thanks. Since last year I have been working on my own UI which is supposed to run under XNA together with a 3D engine. I load the views, styles, themes and (while I was at it) also the 3D scenes for the 3D engine from XAML markup. XAML really becomes easy to understand once you realize it's nothing more than a way to describe how to instantiate and initialize classes.
MohammadIqbal at 20-Jan-12 12:39pm
   
can u solve this
 


<Style TargetType="TextBlock">
MohammadIqbal at 20-Jan-12 12:39pm
   
can u solve this
 


<Style TargetType="TextBlock">
i meant to say C# equival
CDP1802 at 20-Jan-12 14:46pm
   
How about this:
 
Style MyStyle = new Style();
MyStyle.TargetType = "TextBlock";
 
and so on. Of course it only will work if you have a using directive with the namespace where the Style class is located. Nothing hard about it. XAML is just a way to write instructions about which objects to create, what values to put into their properties and how to put them together. You just want to do it manually. Ok, then look up the classes you will need in the MSDN, create them with new() and put them together, Look at the properties and compare them with the attributes in XAML and you will quickly see what you have to put into them. Good Luck.

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

  Print Answers RSS
0 Afzaal Ahmad Zeeshan 256
1 OriginalGriff 251
2 BillWoodruff 240
3 Maciej Los 230
4 Sergey Alexandrovich Kryukov 195
0 OriginalGriff 6,419
1 Sergey Alexandrovich Kryukov 6,008
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,070


Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 20 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