Blogs

The Progress Bar - DevExpress XPF Blog

December 2010 - Posts

  • 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”.

  • Webinar: Writing your first Windows Phone 7 Application

         

    Join DevExpress in a free upcoming webinar where we host Shawn Wildermuth for our first training event covering Windows Phone 7.

    Date:

    January 6th at 10am Pacific Time (GMT -8)

    Event Overview:

    Writing apps for the phone has never been this easy. In this webinar, Shawn Wildermuth will take you from "File | New" to a completed Windows Phone 7 app ready for the Marketplace.

    Presenter:

    Shawn Wildermuth (AgiliTrain)

    Eight-time Microsoft MVP (Data), member of the INETA Speaker's Bureau and an author of six books on .NET. Shawn is currently teaching workshops around the country through his training company AgiliTrain (http://agilitrain.com).

    Audience:

    Level 100

    How to Register:

    Register for this webinar free of charge by visiting the following page: https://www3.gotomeeting.com/register/623904534

  • WPF Project Wizard Update

         

    By now, you’ve had a chance to grab the latest 2010.2 release and also try the updated DevExpress Project Wizard for WPF. In the next upcoming build of 10.2, we’ve added support for creating Ribbon UI as well as improved the product’s usability so that it now displays hints to resolve conflicts as well as suggest recommended scenarios for creating the user interface of an application.

    The following short clip shows the updated Project Wizard in action. You’ll see how a Ribbon UI can be added to the project using the new animated interface:

    DevExpress_WPF_Project_Wizard_Animation

  • Silverlight Grid Control – How to Embed Editors

         

    The Silverlight Grid Control from DevExpress includes over 15 in-place editors that can be used to appropriately format the data as well as provide an interactive and user-friendly way of editing cell values. This is a feature that’s perhaps often overlooked by developers, but very useful for end-users who work with the data displayed in the grid every day. So to that effect, I’ve prepared this step-by-step tutorial on how to embed editors in grid cells.

    Note that for the sake of simplicity, I’m using a list of items as my data source. This is in no way a requirement to using editors with data. Along the way, I’ll also show the code that can be used when working with a grid bound to data using RIA Services.

    1. So to start, I’ll create a new Silverlight Application Project and add a grid control to it.

    2. I’ll bind the grid to the sample list of data through code:

    public MainPage() {
        InitializeComponent();
        grid.DataSource = new ProductList();
    }

    3. I’ll manually create the appropriate columns so it looks like this:

    DevExpress_Silverlight_Grid_Columns

    The XAML used to create this at design-time is as follows:

    <dxg:GridControl x:Name="grid">
        <dxg:GridControl.Columns>
            <dxg:GridColumn FieldName="ProductName">
                <dxg:GridColumn.EditSettings>
                    <dxe:ComboBoxEditSettings DisplayMember="ProductName"
                                            ValueMember="ProductName" />
                </dxg:GridColumn.EditSettings>
            </dxg:GridColumn>
            <dxg:GridColumn FieldName="UnitPrice">
                <dxg:GridColumn.EditSettings>
                    <dxe:SpinEditSettings MaxValue="999" MinValue="1" DisplayFormat="c2" />
                </dxg:GridColumn.EditSettings>
            </dxg:GridColumn>
            <dxg:GridColumn FieldName="Quantity" />
        </dxg:GridControl.Columns>
        <dxg:GridControl.View>
            <dxg:TableView AutoWidth="True" />
        </dxg:GridControl.View>
    </dxg:GridControl>

    4. Note that in the code above, the “ProductName” and “UnitPrice” columns have their column EditSettings property modified. The item inside those tags is the in-place editor. In the case of the ComboBoxEdit, I have specified the DisplayMember as well as the ValueMember properties so the editor knows which field to use to display the data and to populate the combo box.

    The SpinEdit control simply has maximum value, a minimum value and the number’s display format specified.

    5. If I run the application now, you’ll notice that the combo box is embedded inside the cell, but nothing is being displayed and when invoked, it has not been populated with data.

    DevExpress_Silverlight_Grid_Editor_Empty

    This is because while we have specified which fields to get data from, the editor control is unaware of which data source to query.

    6. To fix that issue, I’ll need to also specify where the combo box should get its data from. For this, I’ll add the following line of C# code:

    ...
    using DevExpress.Xpf.Editors.Settings;
    ...
    
    public MainPage() {
        InitializeComponent();
        grid.DataSource = new ProductList();
        ((ComboBoxEditSettings)grid.Columns["ProductName"].EditSettings).ItemsSource = new ProductList();
    }

    7. As an example and a variation of the code above, if I had bound the grid control to a table named “Products” using RIA Services, the code would be as follows:

    ...
    using System.ServiceModel.DomainServices.Client;
    using DXProjectName.Web;
    using DevExpress.Xpf.Editors.Settings;
    ...
    
    private ProductsContext _productsContext = new ProductsContext();
    public MainPage() {
        InitializeComponent();
        LoadOperation<Products> loadOperation = _productsContext.Load(_productsContext.GetProductsQuery());
        grid.DataSource = loadOperation.Entities;
        ((ComboBoxEditSettings)grid.Columns["ProductName"].EditSettings).ItemsSource = loadOperation.Entities;
    }

    8. And that’s it! When I run the application again, the end-result looks like this:

    DevExpress_Silverlight_Grid_ComboBox_Edit

    The project for this example can be downloaded from CodeCentral: http://www.devexpress.com/Support/Center/e/E2603.aspx

  • Silverlight Grid Webinar - Introduction to Data Access and More!

         

    Everyone, if you missed today’s webinar, the recorded version is now available. Click the image below to head over to the DevExpress Channel and watch the video:

    Silverlight_Grid_Webinar_Data_Access

    In this webinar, we covered...

    • How to bind to data using WCF RIA Services (read-only)
    • Create the appropriate infrastructure and code to display a print preview
    • Print and Exporting options available to the DXGrid control
    • How to manually create columns and how to configure unbound columns
    • How to create unbound expressions at design time and modify it at runtime
    • Apply different themes
    • Bind to a list of data through code
    • Right-to-Left Language support

    I have also attached to this post the project which I used for the printing and exporting example. The project of course assumes the presence of the sample Northwind database on a local instance of SQL Server Express, but any data can be used in conjunction with this application.

  • Silverlight Data Grid Control – Data Shaping Features – Video

         

    So our new Silverlight Data Grid Control has been released with the DXperience 2010.2 release and hopefully you’ve had a chance to take a look at it and use some of the advanced features and enjoy the great performance improvements that have been made available.

    The grid control includes a number of data shaping features that can be implemented at design-time or added and customized at runtime. We’ve published a training video that demonstrates how to do the following tasks using the DevExpress Data Grid control for Silverlight:

    • Unbound Columns - XAML and End-User Customization via the built-in Expression Editor
    • Total Summary and End-user customization via the built-in Summary editor
    • Group Summary and end-user customization via the built-in Summary editor
    • Data Filtering (automatic filter row, column dropdown filter, built-in filter editor)

    Click the image below to watch the video on the DevExpress Channel:

    DevExpress_Silverlight_Grid_Data_Shaping_Video

  • Silverlight Grid Lookup Editor Control – Data Binding and Customization

         

    The public release and availability of DXperience 2010.2 presents a great opportunity to download the latest version and try the new products and features available across all platforms. One of these new controls is the Silverlight Grid Lookup Editor. Take a look at the following training video that demonstrates how to bind the lookup editor to data and apply some minor customizations.

    DevExpress_Silverlight_Lookup_Editor_Video

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.