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


MVVM Foundation demo

July 15, 2009

I just added a demo application to the MVVM Foundation project on CodePlex.  It is contrived, but shows how to use ViewModelBase, RelayCommand, PropertyObserver, and Messenger.  You can download the latest source code here.


Announcing the MVVM Foundation library

July 14, 2009

Today I published a new CodePlex project, called MVVM Foundation.  It is a  library of my favorite classes to use in MVVM applications.  Right now, it only contains classes suited for WPF applications, but eventually I will get around to adding in a Silverlight version, too.  If you would like to check it out, here’s the link:

http://mvvmfoundation.codeplex.com/


One way to avoid messy PropertyChanged event handling

July 11, 2009
EDIT: Shortly after publishing this blog post, I received some excellent feedback on it, and updated the PropertyObserver class to implement IWeakEventListener.  Special thanks go to Huseyin Tufekcilerli for posting an IWeakEventListener implementation, which got me 90% of the way toward a better solution. This blog post and its source code download have been updated to reflect this improvement.

When working with objects that implement INotifyPropertyChanged, as most ViewModel objects tend to do, it often becomes necessary to attach a handler to their PropertyChanged event.  Your code can quickly accumulate many event handling methods full of conditional statements that check if e.PropertyName equals some hard-coded string value.  This leads to messy, brittle code that becomes difficult to maintain as the mess gets larger.

To combat this evil, I decided to mechanize as much of this as possible.  I created a generic class called PropertyObserver<TPropertySource> which monitors the PropertyChanged event of an object, and executes whatever callback methods you have registered for when certain properties have changed.  In addition, that class will verify that every property name you register with it is a real, public property on the object being observed, because the way that you provide the property  name is checked by the compiler (you pass it a lambda expression that references the property).  If a property name changes, but you forgot to update your code that registers the property with PropertyObserver, you will get a compiler error next time you try to build your solution.

Here are the two methods of interest:

diagram

Here is a simple example, from the source code download, that shows how to use PropertyObserver:

usage_with_lambda

You can download the source code here.  Note: Change the file extension from .DOC to .ZIP and then decompress it.