Consolidating XAML Namespaces

October 9, 2008

I’m working on a project that has numerous namespaces. Each time I need to use a class in XAML I have to add a new namespace declaration to the XAML file. This quickly becomes unwieldy when you start using multiple namespaces like this:

image

Then of course you have to figure out what namespace each item is in order to use in XAML.

An easy way around this is to consolidate all of the namespaces at the assembly level using the XmlnsDefinitionAttribute from the System.Windows.Markup namespace. In the AssemblyInfo.cs (or .vb) you add this attribute for each namespace you want to consolidate like this:

image

image

There is an optional parameter you can pass in that specifies which assemblies the namespaces live in. This allows you to consolidate namespaces across multiple assemblies.

Now back to our XAML, we can blow out all of these namespace declarations for a much simpler, cleaner approach:

image

kick it on DotNetKicks.com

Advertisements

WPF Record Navigator (VCR) Control

October 5, 2008

I haven’t seen any examples of a record navigator in WPF so I decided to create one.

What it is:

  • Allows you to bind to a collection of elements and navigate through each one.

What it isn’t:

  • Currently it doesn’t have any hooks for styling.
  • The Current Index is read-only and can only be navigated using the buttons.

The implementation is pretty straight forward. It uses routed commands to handle all the navigation. The trick to making the implementation simple was to forward everything on to a ListBox. The template contains a ListBox named “PART_List” which has it’s visibility set to Collapsed. This allows me to bind the TextBlocks that display the Count and Current Position directly to the ListBox. Also for all of the navigation commands, the Command Executed handlers simply forward their calls to the corresponding methods on the ListBox. The ListBox also just binds it’s ItemsSource and IsSynchronizedWithCurrentItem directly to the corresponding Dependency Properties on the RecordNavigator.

I hope this control is useful to some of you. I’ll be further refining it to support styling and allow the user to jump to a specific index. Please give me any feedback you have. If there is any bugs or anything it should do that it doesn’t, please let me know.

Download

You can download the code here: record-navigator. Make sure to change the extension from .doc to .zip.

kick it on DotNetKicks.com