MSDN Magazine Launch Issue - February 15, 2008 - (Page 60) Also, I’ve added an event handler to process any changes to sales data. For example, the following code shows the OnSalesDataChanged method that is invoked if there has been a change in the selection of the current record in the binding source: protected virtual void OnSalesDataChanged(string companyName, string[] x, double[] y) { if (SalesDataChangeEvent != null) SalesDataChangeEvent(this, new SalesSearchEventArgs(companyName, x, y)); } With the selection change, the OnSalesDataChanged event has parameters for the company name, the year, and the sales for the current record. Figure 10 XAML Code Now let’s look at how to create the WPF control and add it to the custom Outlook form region. Two steps are required to add a WPF user control to the custom Outlook Replacement form region. The first is to create the WPF user control, and the second is to add it to the Replacement form region (that is the RecentSales form region created earlier). To create the form, add a new WPF User Control Library project to the solution. Visual Studio will create a default control and display the XAML editor. At this point, you can design a WPF chart control and add event handlers. Figure 9 shows the WPF control in the Visual Studio 2008 XAML designer. Since you’ve added a new project, you’ll have some default XAML code; however, you won’t have anything in the designer other than an empty container. Thus, you need to add some XAML code here to create the chart. To help you along, Figure 10 shows the XAML code I used to create the small WPF sales chart for the form region. The code is fairly straightforward in that it creates a small control with two columns and five rows and puts placeholder text on the chart. This text will be updated with data from the Sales database at run time. Note that the XAML is associated with the SalesChart class as declared in the opening line of XAML code. With the WPF chart created, the last step in the process is to ensure you create methods to update the chart in response to data changes. In the SalesChart class, the UpdateWPFChart method Figure 11 Code to Update Chart public void UpdateWPFChart(string chartTitle, string[] xVals, double[] yVals) { double maxY = 0; foreach (double y in yVals) { if (y > maxY) maxY = y; } try { for (int i = 0; i = xVals.Length) || (i >= yVals.Length)) break; else { TextBlock xText = (TextBlock)(FindName( String.Format(“Category{0}”, i + 1))); if (xText != null) xText.Text = xVals[i]; TextBlock yText = (TextBlock)(FindName( String.Format(“Label{0}”, i + 1))); if (yText != null) yText.Text = yVals[i].ToString(); Rectangle bar = (Rectangle)(FindName( String.Format(“Bar{0}”, i + 1))); bar.Width = (yVals[i] / maxY) * Grid1.ColumnDefinitions[1].Width.Value; } } Title.Text = chartTitle; } catch (Exception ex) { MessageBox.Show(ex.Message, “SalesChart”, MessageBoxButton.OK); } } Adding the WPF Sales User Control 60 msdnmagazine Office Business Applications
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.