[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Silverlight convert XAML to xaml.cs code at runtime

Posted on 2010-01-05
6
Medium Priority
?
995 Views
Last Modified: 2013-12-17
<data:DataGrid.Columns>
    <data:DataGridTextColumn Width="Auto" IsReadOnly="True">
        <data:DataGridTextColumn.HeaderStyle>
            <Style TargetType="dataPrimitives:DataGridColumnHeader">
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <StackPanel>
                                <TextBlock Text=" "/>
                                <TextBlock Text="Department" TextAlignment="Center"/>
                                <TextBlock Text=" "/>
                                <TextBlock Text="Dates" TextAlignment="Center"/>
                            </StackPanel>
                        </DataTemplate>
                  </Setter.Value>
             </Setter>
         </Style>
        </data:DataGridTextColumn.HeaderStyle>
    </data:DataGridTextColumn>
</data:DataGrid.Columns>

Following code can create a column at runtime, but I would like to use multiple line column haeader as shown above.

DataGrid dataGrid1 = new DataGrid();
dataGrid1.AutoGenerateColumns = false;
DataGridTextColumn col1 = new DataGridTextColumn();
col1.Binding = new Binding();
col1.Width = DataGridLength.Auto;
col1.Header = "Auto";
dataGrid1.Columns.Add(col1);

Thanks,
Ashok
0
Comment
Question by:Ashok
  • 3
  • 3
6 Comments
 
LVL 26

Accepted Solution

by:
EDDYKT earned 2000 total points
ID: 26288772
Try

col1.Header = "Department\n" + "Dates";
0
 
LVL 13

Author Closing Comment

by:Ashok
ID: 31673017
You are the man.
0
 
LVL 13

Author Comment

by:Ashok
ID: 26289344
EDDYKT,

Above works, but there is one small problem.
The header is NOT CENTERED.  How do I center it?

Thanks,
Ashok
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 26

Expert Comment

by:EDDYKT
ID: 26289632
how about

using System.Windows.Controls.Primitives;

DataGrid dataGrid1 = new DataGrid();
            dataGrid1.AutoGenerateColumns = false;
            DataGridTextColumn col1 = new DataGridTextColumn();
            System.Windows.Style style = new Style(typeof(DataGridColumnHeader));
            style.Setters.Add(new Setter(DataGridColumnHeader.HorizontalContentAlignmentProperty, (object)"Center"));
            dataGrid1.ColumnHeaderStyle = style;
...
0
 
LVL 13

Author Comment

by:Ashok
ID: 26299316
EDDYKT,

I have already tried that.  What happens is that CENTERING using above code works perfectly as long as HEADER has only one line.
When there are more than one line in the HEADER, all text is initially LEFT-ALIGNED even if I use above code, but if you make the COLUMN width bigger, it moves the TEXT accordingly.  Still perfect CENTERING is not working.
For example,
"Department   "
"ID                  "
Let's say that above is the initial look, and if you make the COLUMN WIDTH bigger,
"      Department                 "
"      ID                                "
That is how it looks like.
Comparing above 2 outputs, it seems like for every 2 spaces added on right side, one space is added on the LEFT side.  In this case, total of three spaces is added.  

Thanks,
Ashok
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 26302799
OK try this

using System.Windows.Markup;

public DataTemplate CreateTemplate(string str1, string str2)
        {
            string str = "<DataTemplate xmlns='http://schemas.microsoft.com/client/2007'><StackPanel><TextBlock Text=\" \"/>";
             str += "<TextBlock Text=\"" + str1 + "\" TextAlignment=\"Center\"/>";
             str += "<TextBlock Text=\" \"/>";
             str += "<TextBlock Text=\"" + str2 + "\" TextAlignment=\"Center\"/>";
             str += "</StackPanel>";
             str += "</DataTemplate>";
             return (DataTemplate)XamlReader.Load(str);
        }

           DataGrid dataGrid1 = new DataGrid();
            dataGrid1.AutoGenerateColumns = false;
            DataGridTextColumn col1 = new DataGridTextColumn();
            System.Windows.Style style = new Style(typeof(DataGridColumnHeader));
            style.Setters.Add(new Setter(DataGridColumnHeader.HorizontalContentAlignmentProperty, (object)"Center"));
            style.Setters.Add(new Setter(DataGridColumnHeader.ContentTemplateProperty, (object)CreateTemplate("Department", "Id")));
            col1.HeaderStyle = style;
...



0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question