Blogs

The Progress Bar - DevExpress XPF Blog

Silverlight Grid, Print Preview, and the Silverlight Window Control

     

So, three product names in the title... what is this blog post going to be about? Here is the quick one sentence summary: Use the Silverlight Window Control from DevExpress to show the print preview of our Silverlight Grid bound to data. That’s the general overview, but this post will also serve as an introduction to showing the print preview, and using the DXWindow and DXLayoutControl, as well as changing themes at runtime.

As always, to get started... we need to set up the infrastructure of the application. To do this, we’ll use the Layout Control from DevExpress.

<dxlc:LayoutControl Name="rootLayoutControl" Orientation="Vertical">
    <dxlc:LayoutGroup>
        <dxlc:LayoutItem Label="Theme:" Name="layoutItem1" HorizontalAlignment="Left" VerticalAlignment="Center">
            <dxe:ComboBoxEdit Name="themeComboBox" IsTextEditable="False"
                              SelectedIndexChanged="themeComboBox_SelectedIndexChanged" />
        </dxlc:LayoutItem>
        <Button Content="Print Preview..." Height="23" HorizontalAlignment="Right" Name="printPreviewButton" 
                VerticalAlignment="Top" Width="150" Click="printPreviewButton_Click" />
    </dxlc:LayoutGroup>
    <dxg:GridControl Name="gridControl1">
        <dxg:GridControl.Columns>
            <dxg:GridColumn FieldName="ProductName" Header="Product Name" />
            <dxg:GridColumn FieldName="UnitPrice" Header="Unit Price" />
            <dxg:GridColumn FieldName="UnitsInStock" Header="Units in Stock">
                <dxg:GridColumn.EditSettings>
                    <dxe:SpinEditSettings MaxValue="999" MinValue="0" />
                </dxg:GridColumn.EditSettings>
            </dxg:GridColumn>
            <dxg:GridColumn FieldName="UnitsOnOrder" Header="Units on Order" />
        </dxg:GridControl.Columns>
        <dxg:GridControl.View>
            <dxg:TableView AutoWidth="True" ShowAutoFilterRow="True" ShowFilterPanelMode="ShowAlways" />
        </dxg:GridControl.View>
    </dxg:GridControl>
</dxlc:LayoutControl>

What do we have here? I’ve first used a layout control and populated it with two layout groups. Both layout groups retain their default view, meaning that they will not display any group headers or tabs. The first group contains a combo box editor (part of the DXEditors library) and a button. We’ll populate the combo box with the themes available and the button will be used to display a print preview of the grid control. Notice that I’ve also quietly embedded a spin edit control for the “UnitsInStock” column.

The print preview will be displayed using the Silverlight Windows Control from DevExpress, or DXWindow. This is a little gem that probably goes unnoticed most of the time, but it can be a valuable tool to provide multiple window functionality inside your web-browser. I’ll set that up outside the root layout control using the following XAML code:

<dxc:DXWindow x:Name="printPreviewWindow" HorizontalContentAlignment="Stretch" IsSizable="True"
              AnimationType="Fade" Width="500" Height="500">
</dxc:DXWindow>

Let’s quickly go over what’s happening here... I’ve created a new DXWindow, specified that it’s content should always be stretched, allowed end-users to resize the window at runtime, and specified the animation type. In this case, the window will fade in when invoked.

If I switch to the designer now, it should look something like this:

DevExpress_Silverlight_Grid_Preview_Designer

Looks a bit weird doesn’t it? Don’t fret... we are on the right track. While you can see the DXWindow control in the background, it will not be displayed in this manner at runtime. We’ll have to invoke it through code. So let’s switch to code view and finish up the rest of the application.

First, I’ll add all the namespace references that I need:

using DevExpress.Xpf.Core;
using DevExpress.Xpf.Printing;
using DevExpress.Utils;
using DevExpress.Xpf.Printing.DataNodes;
using System.ServiceModel.DomainServices.Client;
using SolutionName.Web;

Next, I’m going to take care of the data binding for the grid. For this example, I’m using RIA Services to bind to a sample database hosted on a local instance of SQL Server Express. For step-by-step instructions on how to do this, watch my video on this topic.

private ProductsContext _productsContext = new ProductsContext();

private void populateGrid() {
    LoadOperation<Products> loadOperation = _productsContext.Load(_productsContext.GetProductsQuery());
    gridControl1.DataSource = loadOperation.Entities;
}

I’ll now create a method to populate the themes combo box and then handle it’s SelectedIndexChanged event to set themes at runtime:

private void populateThemes() {
    themeComboBox.Items.Add("LightGray");
    themeComboBox.Items.Add("DeepBlue");
    themeComboBox.Items.Add("Office2007Blue");
    themeComboBox.Items.Add("Office2007Black");
    themeComboBox.Items.Add("Office2007Silver");
    themeComboBox.SelectedItem = "Office2007Blue";
}

private void themeComboBox_SelectedIndexChanged(object sender, RoutedEventArgs e) {
    ThemeManager.ApplicationThemeName = themeComboBox.SelectedItem.ToString();
}

The next method will generate the print preview, add it to the window and display the DXWindow control. All of this will be invoked when the “Print Preview” button is clicked:

private void printGrid() {
    DocumentPreview preview = new DocumentPreview();
    VisualDataNodeLink link = new VisualDataNodeLink(gridControl1.View as IRootDataNodeSource);
    link.ExportServiceUri = "../ExportService1.svc";
    link.PrintingSystem.ExportOptions.Html.EmbedImagesInHTML = true;
    LinkPreviewModel model = new LinkPreviewModel(link);
    preview.Model = model;

    // Setup the DXWindow and content
    printPreviewWindow.Title = "Grid Print Preview";
    printPreviewWindow.Show();
    printPreviewWindow.Content = preview;

    link.CreateDocument(true);
}

private void printPreviewButton_Click(object sender, RoutedEventArgs e) {
    printGrid();
}   

Finally, to make sure that the grid and combo box are populated and a default theme is set, I’ll modify the page’s initializer in the following manner:

public MainPage() {
    ThemeManager.ApplicationTheme = Theme.Office2007Blue;
    InitializeComponent();
    populateThemes();
    populateGrid();
}

And that’s it! Let’s run the application to see it in action:

Silverlight_Grid_Spin_Editor

In the above clip you see the in-place spin editor control in action. The runtime theming functionality works as well, applying the selected theme to all the components on the page:

Silverlight_Grid_Themes

And finally, the print preview in the DXWindow control:

DevExpress_Silverlight_Grid_Print_Preview_Window

The project containing all the code is also attached to this post as “SampleProject.zip”.

Published Dec 28 2010, 12:45 PM by Emil Mesropian (DevExpress)
Attachment: SampleProject.zip
Bookmark and Share

Comments

 

Alan Anderson said:

I am having difficulty getting grid exporting working from my Silverlight project, so I am very interested in this topic.

The discussion says "The project containing all the code is also attached to this post." Unfortuately, I cannot find it. Where is the "Silverlight Grid, Print Preview, and the Silverlight Window Control" sample source code hidden?

Thanks, Alan

December 28, 2010 4:04 PM
 

Alan Anderson said:

Nevermind my previous question about where is the sample source code, I found the link/attachment. But I have another problem. When I run the sample sandbox application the grid prints as expected. Unfortunately, when I click on the export button in the print preview window an error message box appears, saying "The remote server returned and error: NotFound.". This happens when attempting to export to either PDF or XLS formats. I have not been able to track down the exporting problem with this sandbox sample, can you please provide insight and help?

Thank you! Alan

December 28, 2010 5:24 PM
 

Emil Mesropian (DevExpress) said:

Hi Alan,

To keep this example simple and focused around theming, print preview and the DXWindow, I did not implement the export service. I will cover this in detail in another blog post. In the meantime, you may look at the corresponding document in our help system on how to implement exporting: documentation.devexpress.com

Let me know if you have any questions.

Emil.

December 28, 2010 5:38 PM
 

Emil Mesropian (DevExpress) said:

@Alan:

I've updated the attached project to include the export service. I'll make sure to cover this in detail in an upcoming blog post, but for now, you'll have a working example.

Emil.

December 28, 2010 5:43 PM
 

Alan Anderson said:

Emil,

Thank you very much for the grid printing with export sample! It set me on the right track. What was not apparent from looking at various separate example is that the Silverlight-Enabled XtraReports Service alone can handle both printing and exporting.

Keep up the good work,

Alan

P.S. On my system the example crashes if I use the combobox to change the theme multiple times. Not a big deal for me, because I am not going to let users change the theme; but I thought you may like to know...

December 29, 2010 12:46 PM
 

The Progress Bar - DevExpress XPF Blog said:

In my previous blog post , I provided an introduction to binding the grid, applying themes, and displaying

January 3, 2011 6:47 PM
More from DevExpress
Live Chat
Have a pre-sales question?
Need assistance with your evaluation?
We are here to help.
Chat is one of the many ways you can contact members of the DevExpress Team. We are available Monday-Friday between 8:30am and 5:00pm Pacific Time.
If you need additional product information, require pre-sales assistance, or want help with your order, write to us at info@devexpress.com or call us at
+1 (818) 844-3383.