Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
HTML
<DataGrid.Columns>
                <DataGridTextColumn Header="AuthorID" FontWeight="Black" Foreground="Red"
                                       FontSize="16" FontStyle="Italic" 
                                       CanUserReorder="True"  
                                       CanUserSort="True" SortDirection="Ascending">
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="TextBlock">
                            <Setter Property="TextAlignment" Value="Center" />
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                    <DataGridTextColumn.EditingElementStyle>
                        <Style TargetType="TextBox">
                            <Setter Property="TextWrapping" Value="Wrap" />
                            <Setter Property="AcceptsReturn" Value="true" />
                        </Style>
                    </DataGridTextColumn.EditingElementStyle>

                <DataGridTemplateColumn Header="DOB" MinWidth="100" CanUserSort="False">
                    <DataGridTemplateColumn.HeaderStyle>
                        <Style TargetType="{x:Type 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>
                    </DataGridTemplateColumn.HeaderStyle>
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <DatePicker SelectedDate="{Binding DOB}" SelectedDateFormat="Short" CalendarStyle="DynamicResource CalenderControlTemplate" removed="Yellow" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding DOB, StringFormat=d}" Foreground="DarkTurquoise" HorizontalAlignment="Center" />
                        </DataTemplate>
                    </dg:DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridCheckBoxColumn Header="Pass/Fail" Binding="{Binding Pass}">
                    <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>
                    </DataGridColumn.HeaderStyle>
                </DataGridCheckBoxColumn>

</DataGrid.Columns>


This code gives a textblock column, calender type column ,and check box column at the design time . my question is how i can generate the same situation at run time using codebehind. in fact I require C# equivalent of above code .

any help is appreciated

Thanks in advance

Iqbal
Posted

If you go to the MSDN documentation for each class and property in your XAML, then you will see the C# equivalent. Much of it is easy to figure out for yourself. See here[^] for a sample.
 
Share this answer
 
Comments
[no name] 26-Jan-12 9:13am    
But I am not getting much grip on that can you give a sample how a calender column generate at runtime
Richard MacCutchan 26-Jan-12 10:10am    
What is a Calendar Column?
Why not just forget the WPF and go back to pure C# and build your project from that. Using a simple Windows form the designer will generate the code for you. Alternatively Espen has given you a useful answer in his solution.
Inside the obj folder of your project there will be a *.g.cs file for your xaml file - Most of what you are looking for will proably be there.

To add a column at runtime do the following:
DataGridTextColumn textColumn = new DataGridTextColumn();  
dataColumn.Header = "First Name";  
dataColumn.Binding = new Binding("FirstName");  
dataGrid.Columns.Add(textColumn);


Update
DataGridTemplateColumn col = new DataGridTemplateColumn(); 
col.Header = "Date"; 
 
// Create a factory. This will create the controls in each cell of this 
// column as needed. 
FrameworkElementFactory factory = 
    new FrameworkElementFactory(typeof(DatePicker)); 
 
// Bind the value of this cell to the value of the Date property of the 
// DataContext of this row. The StringFormat "d" will be used to display 
// the value. 
Binding b = new Binding("Date"); 
b.StringFormat = "d"; 
factory.SetValue(DatePicker.SelectedDateProperty, b); 
 
// Create the template itself, and add the factory to it. 
DataTemplate cellEditingTemplate = new DataTemplate(); 
cellEditingTemplate.VisualTree = factory; 
 
col.CellEditingTemplate = cellEditingTemplate; 


Best regards
Espen Harlinn
 
Share this answer
 
v3
Comments
[no name] 26-Jan-12 9:49am    
but to define a calender column what I have to do
[no name] 26-Jan-12 10:23am    
i am very much thankful to you but can you clarify this "dataColumn.Binding = new Binding("FirstName"); " whether this statement bind Column called "FirstName " in the database to DataGrid
Espen Harlinn 26-Jan-12 10:26am    
In a manner yes, but it can also be the name of a property of an element in a collection. WPF binding is quite flexible.
best solution for this is
C#
string xaml="http://Schemas.microsoft.com//winfx//2006//xaml//presentation"+
            "xmlns:x=http://Schemas.microsoft.com//winfx//2006//xaml"+
           "<ur xaml code>";

XmlReader xml =XmlReader.Create(new StringReader(xaml));
UIElement element =(UIElement)XamlReader.Load(xml);

<basecontrol>.Children.Add(element);
</basecontrol></xaml>


this can b used to run any xaml code at runtime
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900