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

Why Can't This Be Fixed?

5/28/2009 10:45:00 AM

I’ve been running some flavor of Win 7 for awhile now.  My problem still exists.  In fact, it has existed for as long as you have been able use plug-in-play drive devices.  You’ve probably experienced as well.  It has probably driven you a bit batty like it has me (along with a myriad of other things, many of which are not PC related).  I’ve submitted complaints, suggestions, bitches… you name it, but the problem remains the same.  You might have done the same.

What am I talking about? That FREAKIN’ (I would use another word that begins with F but this is a family blog) dialog box that pops up when you want to disconnect a removable hard dive that says that you can’t remove the device because some other process/application is using that drive.  Well, that is nice to know, but you you please tell me what F’ING application is so I can close it.  When I have 20 different windows open, and some may or may not or maybe did but are not currently using that drive, it is a PIA figuring out which one it is.  God forbid the app is actually a system tray app that Win 7 now conveniently hides the icon for.  ARGHHHHH!!!!!!!!!!!!!!

Can’t the OS say something like, “Sorry, but the DoofinSchwark application is currently using this device.  Close this application if you wish to remove the device.” or something equally helpful.  It’s the little things….. like knowing how to merge onto a busy highway.  Yeah, I’m talking to you jack @$$!!!!

Tags:

Rant

New Spaghetti Code Podcast: Javier Lozano and Code Frameworks

5/27/2009 9:29:00 AM

Spaghetti Code Talks Code Frameworks with Javier Lozano about the various open source code frameworks he used in the application he is currently developing.  Covering things like nHibernate, Windsor and more, Jav talks about the frameworks he used, the rationale for picking them, and how they helped him create his application fast and with more flexibility and reliability.

  • Direct Download - click here
  • Subscribe - click here
  • iTunes - click here
  • Tags:

    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

    New Spaghetti Code Podcast: Silverlight 3 with Adam Grocholski

    5/18/2009 7:05:15 AM

    Adam Grocholski returns to talk about what’s new in Silverlight 3 and other web technologies unveiled at the MIX Conference.  Starting of with a discussion of Silverlight Out-of-the-Browser the conversation quickly moves to other areas of Silverlight 3 and more.

  • Direct Download - click here
  • Subscribe - click here
  • iTunes - click here

  • Tags:

    It is a Proud Day

    5/12/2009 7:44:00 AM

    Well, as I push through the sub-200 pound barrier in my weight loss program, I thought it was time to see what the Internet thought of me.  As my three long time followers already know (get a life already!), I am the world’s first, and as far as I know only, .NET Sex Symbol.  Even though it is a self-proclaimed title, it has been a long, hard journey to achieve world wide recognition, and it has not been as easy or as glamorous as you probably think it is. 

    So, without further ado, I am proud to proclaim that this web site, this humble blog, now shows up as the top listing on three of the four major search engines when you search for “.net sex symbol” – and the quotes are not required!  BTW, Yahoo – you suck!

    image

    image

    I’m very proud of this achievement and would like to thank everyone that has made it possible.  I would especially like to thank the Microsoft marketing department for not changing the .NET name brand to something else!  The Microsoft Windows Code Development and Deployment Runtime Environment for Scalable Applications Sex Symbol just doesn’t have the same ring to it.

    Tags:

    Junk

    New Spaghetti Code Podcast: UI Patterns with Adam Grocholski

    5/11/2009 12:33:03 PM

    After a bit of a hiatus, Spaghetti Code returns with a discussion of UI patterns with Adam Grocholski.  Starting of with a brief discussion of patterns, the conversation then moves into a review of the various UI patterns including MVC, MVP and MVVM.

  • Direct Download - click here
  • Subscribe - click here
  • iTunes - click here

  • Tags:

    SpaghettiCode

    TwitLook Pre-Pre-Alpha Available

    5/2/2009 7:38:24 PM

    I’ve posted the binaries for TwitLook.  I’ll get the source out once I get it cleaned up – I’m hacking around trying some different things and learning more about WPF at the same time so it is a bit ugly in there right now.

    Tags:

    TwitLook

    Microsoft Vine Beta

    5/1/2009 4:37:28 PM

    I hadn’t seen this before – Microsoft Vine Beta.  It looks like a cross between Twitter, news feeds, and some geo-location stuff.  You can only ask to be in the beta right now, so I threw my email address in and have my fingers crossed.  There is a video, however, that shows the general application and key concepts.  I couldn’t find a way to run the vid in full screen, so that was an absolute FAIL as I couldn’t tell exactly what was going on.

    Tags:

    Slick Thoughts

    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

    <<  May 2013  >>
    MoTuWeThFrSaSu
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    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 2013

    Sign in