Storing Application-wide Data in WPF

It is very common in Windows applications to store application-wide data such as database connection strings, application title and folder path,etc that should be available for the duration of the application instance. The general strategy for storing such data is to have public classes with public properties/fields and access them from anywhere in the application. However, with Windows Presentation Foundation (WPF), the framework itself provides an application-wide “storage bag”, Application.Properties, that could be used for the very same purpose. This bag is an app-domain specific thread-safe key-value based IDictionary instance.

using System.Windows;
... ...
Application.Current.Properties["conStr"] = "my connection string";
... ...
string conStr = (string) Application.Current.Properties["conStr"]; // From anywhere in the application


Since the key value is of type object, a casting is required when retrieving the data from Application.Properties.

Static Breadcrumb with SiteMapPath Control

Today I saw a question in the ASP.NET newsgroup asking about
implementing a static (non-hyperlinked) site map path. As you know, the SiteMapPath
control in ASP.NET 2.0 displays a breadcrumb showing the current spot in the
site map navigation defined in the web.sitemap XML file. Parent nodes in
the current navigation path are shown as links to the respective pages, each
separated by the path separation character (defined by the SiteMapPath.PathSeparator
property). However, if you would like a static breadcrumb, that is, without
making parent nodes as links but as plain text, just hook into the
ItemDataBound event of the SiteMapPath control and clear the navigation URL (originally taken from the web.sitemap file) for every node.

Assuming a simple SiteMapPath markup as below:



<asp:SiteMapPath ID="SiteMapPath1" runat="server" CurrentNodeStyle-Font-Bold="true" NodeStyle-Font-Size="Small" OnItemDataBound="SiteMapPath1_ItemDataBound" PathDirection="RootToCurrent" PathSeparator=" > " PathSeparatorStyle-Font-Size="Small" RenderCurrentNodeAsLink="false">

Have the following for the ItemDataBound event:



protected void SiteMapPath1_ItemDataBound (object sender, SiteMapNodeItemEventArgs e)
{
  {
    if (e.Item.ItemType == SiteMapNodeItemType.Parent || e.Item.ItemType == SiteMapNodeItemType.Root)
    {
      // Control index may vary if additional controls have been defined in the node template
      ((HyperLink)e.Item.Controls[0]).NavigateUrl = "";
    }
  }
}

The result would look something like this (assuming an apporpriate web.sitemap file is in place):





Without this change, the same SiteMapPath control would look like (parent pages hyperlinked):