Karl Shifflett’s Magnum Opus

February 8, 2010

My good friend and comrade in crime, Karl Shifflett, recently published a huge body of excellent work.  He has been working for quite some time on Ocean, a code generation system that blows my mind, and BBQ Shack, a great line-of-business WPF and Silverlight application that is built by Ocean.  His prolonged effort has resulted in some high-quality software and documentation (including videos!).

I highly suggest you set aside some time to check out what Karl has been up to.  It’s amazing work.

My hat is off to you, Karl!


Using a ViewModel as a value converter

January 8, 2010

A recent post by Josh Twist shows how to support mingling code in XAML for WPF devs.  There have been several examples of putting code into XAML over the years, and they always raise the discussion of whether or not it is a good practice.  I am usually against having code in XAML because it makes it difficult to debug and maintain, but to each his/her own.  Regardless, that post got me thinking…

For a while now I’ve wondered how one might go about making it so that the little bits of code in the XAML might somehow be transplanted into the highly testable, debuggable, lovable world of the ViewModel.  Sure, you could put this type of logic into value converters, but writing a value converter that is only used once seems like a lot of extra work for little benefit.

Just for kicks, I decided to implement a class that would allow you to specify a method on your ViewModel object that contains the code that would otherwise be placed into a value converter, or in XAML.  The result is a markup extension I ever so lazily named BindingEx.  Here’s a simple usage:

Notice the last bit where the ConvertMethod property is set to AdjustTextWidth.  This is how you specify which method on the element’s VM should be invoked when the window’s width changes.  Here’s the VM class:

The TextViewModel object halves the window’s ActualWidth, and that value ends up being the width of the TextBlock.

I’m not sure that anyone should ever use this technique in a real application.  It’s probably a very stupid idea altogether, so I want to avoid any bad karma by stressing the point here:  USE AT YOUR OWN RISK!!

If you want to check out how I implemented BindingEx, click here to download the source code.  Be sure to change the file extension from .DOC to .ZIP and then decompress it.


The ultimate hack for Silverlight in Blend

January 6, 2010

I recently needed to write some design-time support for a Silverlight framework.  I needed to discover a solution’s assemblies while running in Blend, and then pass those assemblies downstream for further processing.  In WPF, using the full .NET framework, that’s easy: just call AppDomain.CurrrentDomain.GetAssemblies() and then filter them based on the Location property.  In Silverlight, however, a couple of problems showed up that seemed to make this task impossible.

First, the Assembly.Location property is marked ‘Security Critical’ in Silverlight 3.  This means that if you access the property, an exception will be thrown.

Second, Silverlight 3’s AppDomain class does not have a GetAssemblies method!

It turns out that both of these problems are easy to overcome.  The key thing to know is that when Silverlight code is running in Blend, it’s actually executing against the regular .NET Framework (the same one that WPF uses).  You can access ‘Security Critical’ members and no errors occur.  That takes care of the Assembly.Location issue.  Even though the Silverlight code you write, that is intended to run in Blend, can only access the Silverlight API, you can still access the full .NET framework’s API via reflection.  This allows you to work around the second issue.  For example:

var assemblies = typeof(AppDomain)
   .GetMethod("GetAssemblies")
   .Invoke(AppDomain.CurrentDomain, null);

Remember, this hack only works when your Silverlight code is running in Blend! This will not work when running under normal circumstances, since Silverlight apps normally only have access to the Silverlight subset of the .NET Framework.

Happy coding!


Updates made to WPF graphing application

November 17, 2009

I recently published an article on CodeProject that reviews a WPF graphing application.  It turns out that my original circular dependency detection algorithm did not detect all circles in certain scenarios.  I completely rewrote that algorithm, so that it now properly detects all circles in any object graph.

I also modified the way that the node connectors move, so that it feels more responsive and elastic.  The app is much better now, and the article has been updated to reflect these changes (including a new source code download).  If you want to check it out, here’s the link:

http://www.codeproject.com/KB/WPF/WpfGraphVisualization.aspx


WPF graph that detects circular dependencies

November 16, 2009

I spent the weekend having a blast writing a WPF app that displays an interactive object graph, and detects/highlights circular dependencies. It was so much fun, that I decided to publish an article about it on CodeProject.

Here’s a screenshot of the app:

ComplexCircularGraph

The app uses Charles Petzold’s ArrowLine element, PropertyObserver and ObservableObject from my MVVM Foundation library, DragCanvas from WPF.JoshSmith, and ContentControl3D from Thriple.

If you want to check it out, here’s the article:

http://www.codeproject.com/KB/WPF/WpfGraphVisualization.aspx

Enjoy!


XAML Tip: DataContext Comment

October 24, 2009

When working on large WPF or Silverlight applications, you can end up with a lot of XAML files.  If you are using the MVVM pattern, many of those XAML files contain a View whose DataContext is expected to be set to a certain type of ViewModel object.  It can sometimes become difficult to remember which Views expects which ViewModel as their DataContext.  One very simple and lightweight way to help you and your team remember a View’s expected DataContext type is to leave an XML comment at the top of the XAML file.  This incredibly simple and obvious technique can be a HUGE timesaver later on down the road!

datacontext

I highly suggest that you get into the habit of doing this, if you haven’t done so already.  You will thank yourself many times later…


Quick and dirty WPF element tree viewer

September 30, 2009

This morning I decided to build a little WPF dialog window that would show the visual tree of some UI, and provide visual indicators over that UI when you select one of its elements.  This was just a fun exercise.  If you want to have a serious element tree viewer, then be sure to use Snoop instead.  This quick post just shows the result of my morning fun.

Here’s the resulting dialog window in action:

snooper

Notice that a ContentPresenter is selected, which contains an image of a ninja.  In the UI being analyzed, you’ll see that the ContentPresenter is decorated with a light green box:

demowindow

The “Snooper” dialog window contains a TreeView that renders the visual tree of the other UI.  That TreeView is declared as:

treeview

It is bound to a hierarchy of VisualElement objects.  VisualElement is a class that I made to represent an element and its child elements.  The important part of that class is seen below:

visualelement

Notice that when a VisualElement is selected, it puts a SelectionAdorner into the adorner layer of its associated UIElement.  SelectionAdorner is a class I made that just renders a rectangle around some element.  It is seen below:

adorner

You can download the demo project here.  NOTE: Be sure to change the file extension from .DOC to .ZIP and then decompress.  WPF is fun!


The “From Russia with Love” way to create ViewModels

September 7, 2009

I just published an article about a technique I use to streamline and simplify the way that ViewModels are created in an MVVM app.  I’m not saying that this is the “right” way to do things, but simply something I find useful.  Enjoy!

http://www.codeproject.com/KB/WPF/FromRussiaWithLove.aspx


How to prevent a TabItem from being selected

September 4, 2009

In case you ever need to prevent the user from selecting a tab in a WPF TabControl, here’s one way to do it…

ControllingTheTabControl

The SelectedContent of the TabControl hasn’t changed at the time that the ItemsSource’s default collection view raises its CurrentChanging event.  If you decide to lock the user into the selected tab, simply set the TabControl’s SelectedIndex back to the index of the SelectedContent.  No fuss, no muss…


Updates made to MVVM Foundation

August 23, 2009

I set aside some time today to add some goodness, and remove some badness, from my MVVM Foundation library on CodePlex.  Special thanks go to Matt Heffron for his great work on making the Messenger class much safer and user-friendly.  Here is a listing of the changes:

  • Added safety checks and helpful error messages in Messenger for callback registration and message broadcasting. Unit tests were added to verify this behavior.
  • Ensured that Messenger notifies colleagues of a message in the order they registered for that message. Unit tests were added to verify this behavior.
  • Fixed an issue in an ObservableObject unit test where it failed in a Release build.

It’s exciting for me to see the improvements and ideas suggested by people using MVVM Foundation!  I appreciate the time people have contributed toward making the library better.  I hope there are no hard feelings when I reject a proposed feature.  :)

You can get the latest source code here: http://mvvmfoundation.codeplex.com/SourceControl/ListDownloadableCommits.aspx