Tabs Studio Blog (organizing Visual Studio document tabs)

September 2, 2009

Using attached properties in an add-in for advanced styling

Filed under: Uncategorized — Tags: , — Sergey Vlasov @ 9:15 am

Currently in style triggers only predetermined in Tabs Studio (IsDocument, IsTabSelected) and in WPF (IsMouseOver) properties can be used. For the next Tabs Studio version I’ve added ability for an add-in to add and set its own custom properties to any tab element and use it in style triggers.

These custom properties are WPF Attached Properties. An add-in should define custom static DependencyProperty in a class (I recommend to use separate dedicated class for it), initialize this property using the RegisterAttached method and define two static methods to set and get the property.

After a custom property is defined and registered, an add-in can set it for any tab element when needed:

Properties.SetIsTopProject(tab.TabItem, isTopProject);

Where Properties is a class containing the property, SetIsTopProject is a static set method for the property, tab.TabItem is a tab element we set property for, isTopProject is a value for the property.

On the style side we use this new property in a trigger almost as usual:

<Trigger Property="TabsStudioDecorator:Properties.IsTopProject" Value="True">

We must additionally specify the XAML namespace and the class where the property is defined. Defining this custom XAML namespace is an add-in’s job. Main add-in class must implement the TabsStudioExt.IStyler interface – return a string from the GetNamespacesForResourceDictionary method with required namespace(s) definition(s):

public string GetNamespacesForResourceDictionary()
{
    return "xmlns:TabsStudioDecorator=\"clr-namespace:TabsStudioDecorator;assembly=Decorator\"";
}

Blog at WordPress.com.