Building a Composite WPF Application Part 9: Intro to Event Publishing and Subscribing

6/5/2009 4:05:00 PM

In this installment, we take a look at using PRISM events to publish and subscribe to events. Previous versions of the application have used .NET events and event handlers to communicate UI events between presenters. We know use PRISM events in conjunction with WPF Commands (introduced earlier) to notify various parts of the application that something important has happened.

 

Download video here

Download source code here

Tags:

WPF Composite App

Building a Composite WPF Application Part 8: Custom Commands and Non-Command UI Elements

5/28/2009 4:26:00 PM

This installment looks at extending the Command concept to controls in WPF that do not natively support Commands.  This allows developers to work inside of the WPF Composite App Framework (PRISM) to connect Commands to controls other than just ICommandSource controls (Button being the prime example).

 

Download video here

Download source code here

Tags:

WPF Composite App

Building a Composite WPF Application Part 7: Intro to Commands

5/28/2009 4:18:00 PM

This installment looks at using Commands inside of your WPF Composite App using PRISM.  This is a very simple example of how to use Commands and leveraging the DelegateCommand that the PRISM framework provides. 

 

Download video here

Download source code here

Tags:

WPF Composite App

Visual Tree Printing in WPF Applications

5/26/2009 9:22:48 AM

HorseCalc It’s been a while since I have posted, so I thought I would whip out a quick nugget while I work on a longer technical post.  I have been hacking away at a side project and experimenting with Prism 2 and MVC/MVP/MVVM/et al design patterns and came upon the need to print some of the output from that was already on the screen.  This seemed like a somewhat common occurrence for Composite style applications – think “print THAT widget”. The screenshot shows an application I have been working on in my spare time and the red area shows the portion of the screen I would like to print.  I quick, and admittedly cursory, examination of the SDK provided lots of examples on generating custom output to the WPF XPS infrastructure, but it was all about dynamically generating the print content.  I already had the content I wanted to print, why did I need to go through a lot of hoops and additional code just to print something I had already created?

What I was really looking for was a way to print WYSIWYG style.  I wanted to take just the chart portion of the UI and send it to the printer.  A web search got me close - “Printing WPF window(visual) to printer and fit on a page”. The problem was that Pankaj’s solution was printing the entire window, and not just a smaller part of the overall visual tree.  Fortunately, the solution is pretty straightforward but I thought I would post it anyway.  So standing on the shoulder of giants, let me show you what I did.

First, Horse.NET is built on a flavor of MVP, so I have a button on the screen (actually the View for the SummaryPresenter) that initiates the printing process.  Here is the XAML for that button.

   1: <Button Content="Print" Command="{Binding Path=PrintCommand}" CommandParameter="{Binding ElementName=ReportPanel}"></Button>

There are two important things to note here. First, I am using a WPF command to start the printing process.   You don’t have to do it this way, but it lets me tie the presenter to the UI pretty cleanly.  The second thing is the CommandParameter.  It is passing in a reference to the the ReportPanel.  ReportPanel is just a WPF Grid control that wraps the title  TextBlock and a Listbox that contains the actual charts.  The simplified XAML is below:

   1: <Grid x:Name="ReportPanel" > 
   2:     <Grid.RowDefinitions> 
   3:        <RowDefinition Height="Auto" /> 
   4:        <RowDefinition Height="*" /> 
   5:     </Grid.RowDefinitions> 
   6:     <TextBlock /> 
   7:     <ListBox/>> 
   8: </Grid>

With that UI established, lets jump to the code.  When the user clicks the Print button, the following WPF command is executed:

   1: this.PrintCommand = new SimpleCommand<Grid> 
   2: { 
   3:     CanExecuteDelegate = execute => true, 
   4:     ExecuteDelegate = grid => 
   5:         { 
   6:             PrintCharts(grid); 
   7:         } 
   8: };

This is pretty simple stuff.  SimpleCommand implements the ICommand interface and lets me pass in some lambda expressions defining the code I want to run when this command is fired.  Clearly, the magic happens in the PrintCharts(grid) call.  The code shown below is basically the same code you would find in Pankaj’s article with a couple of modification highlighted in red. 

   1: private void PrintCharts(Grid grid) 
   2: { 
   3:     PrintDialog print = new PrintDialog(); 
   4:     if (print.ShowDialog() == true) 
   5:     { 
   6:         PrintCapabilities capabilities = print.PrintQueue.GetPrintCapabilities(print.PrintTicket); 
   7:  
   8:         double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / grid.ActualWidth, 
   9:                                 capabilities.PageImageableArea.ExtentHeight / grid.ActualHeight); 
  10:  
  11:         Transform oldTransform = grid.LayoutTransform; 
  12:  
  13:         grid.LayoutTransform = new ScaleTransform(scale, scale); 
  14:  
  15:         Size oldSize = new Size(grid.ActualWidth, grid.ActualHeight); 
  16:         Size sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight); 
  17:         grid.Measure(sz); 
  18:         ((UIElement)grid).Arrange(new Rect(new Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), 
  19:             sz)); 
  20:  
  21:         print.PrintVisual(grid, "Print Results"); 
  22:         grid.LayoutTransform = oldTransform; 
  23:         grid.Measure(oldSize); 
  24:  
  25:         ((UIElement)grid).Arrange(new Rect(new Point(0, 0), 
  26:             oldSize)); 
  27:     } 
  28: }

All right, what are these modifications?  The most obvious is that I am replacing the use of the original this object (which represented the entire application window in the original code) with the Grid control that was passed in as part of the Command.  So all of the measurements and transforms are executed using the Grid.  The other change is that I have save the original Transform and Size of the Grid as well.  The reason is that when you transform the Grid to fit to the printing page, it causes the actual application UI to change as well.  This doesn’t look so good on your screen, so after sending the Grid to the printer, I transform it back to its original screen layout. 

There you have it.

Tags:

WPF Composite App | Library

Building a Composite WPF Application Part 6: Working with Models

4/7/2009 11:36:00 AM

In this installment of the Building a Composite WPF App (Prism) series, I take a look at building models. I show why we may build View specific models and how they will be exposed by the Presenters in the Northwind Business Center application.  I say this in the screencast, but it is very possible to combine the presenter and the model into a single ViewModel, but I have kept them separate for illustration purposes. 

 

Download video here

Download source code here

Tags:

Screencasts | WPF Composite App

Building a Composite WPF Application Part 5: Building a Basic Presenter

4/6/2009 5:02:00 PM

The next installment of the Building Composite WPF app series is now available.  In this installment, I introduce building a simple View implementation in the MVP or MVVM style. The demo illustrates creating simple view that will be used to display a list of customers in our Northwind Business Center application.

 

Download video here

Download source code here

Tags:

Screencasts | WPF Composite App

Building a Composite WPF Application Part 4: Intro to Views

4/6/2009 4:16:00 PM

The next installment of the Building Composite WPF app series is now available.  In this installment, I introduce building a simple View implementation in the MVP or MVVM style. The demo illustrates creating simple view that will be used to display a list of customers in our Northwind Business Center application.

 

Download video here

Download source code here

Tags:

Screencasts | WPF Composite App

Building a Composite WPF Application Part 3: Intro to Modules

1/23/2009 8:05:00 PM

The next installment of the Building Composite WPF app series is now available.  In this installment, I introduce Modules by providing an overview of what they are and an example of the steps involved in creating one.  The demo illustrates a simple view from a Module being displayed in our Northwind Business Center application.

 

Download video here

Download source code here

Tags:

Screencasts | WPF Composite App

Building a Composite WPF Application Part 2

11/5/2008 2:48:00 PM

The next installment of the Building Composite WPF app series is now available.  In this installment, I give a quick overview of what Regions are in the Composite App Library and how they function.  I then add three regions to the Northwind Business Center application, demonstrating what it looks like inside of Visual Studio.  We still don’t have any real functionality yet, but we have all the scaffolding in place to start working on our first module and associated views and functionality in the next installment.

I also used a new lead in which is basically the old lead out moved to the front followed by the session title.  Not sure if I should stick with the old “title slide” type of intro or not.

 

Download video here

Tags:

WPF Composite App

Building a WPF Composite Application Series Part 1

10/14/2008 11:27:00 AM

Over the next few weeks, I will be building a WPF application using the WPF Composite Application Library (CAL).  With each screencast, I will be introducing a new concept from the CAL and showing how it is used in our application.  Over the course of the series, we will be creating the Northwind Business Center application which will expose a customer information, order information, inventory, etc.  The application will use modules, views, and application services using the CAL.

Part 1 introduces the CAL and shows the steps to get the application foundation set up in Visual Studio.  To get started, you will need to have downloaded and installed the WPF Composite Application Library and the Northwind Sample Database.  The sample application will use SQL Server but you can change the connection strings to work against SQL Express if that is the route you want to go. 

 

Download video here

Tags:

WPF Composite App

Powered by BlogEngine.NET 1.6.0.0
Theme by Mads Kristensen

About the author

Jeff Brand Jeff Brand

This is the personal web site of Jeff Brand, self-proclaimed .NET Sex Symbol and All-Around Good guy. Content from my presentations, blog, and links to other useful .NET information can all be found here.

E-mail me Send mail


Calendar

<<  August 2014  >>
MoTuWeThFrSaSu
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

View posts in large calendar

My Twitter Updates

XBOX
Live

Recent comments

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2014

Sign in