a·gen·tic a·gil·i·ty

Composite WPF and Merged Dictionaries

TL;DR; Explains how to manage dynamic resource dictionaries and themes in WPF with Composite WPF, addressing common errors and providing code solutions for region management.

Published on
3 minute read
Image
https://nkdagility.com/resources/rVJ3HWMCFYD
Subscribe

If, like me, you like to have changeable resource files in your application. Wither it is for changing the Theme, or interchanging templates you will need to take special care when using the

libraries.

The new version of the TFS Sticky Buddy uses both!

Composite WPF and Merged Dictionaries

I am using the built in Infragistics theme system, and the first time you select a theme I am just adding a resource file that overrides the default:

1   1: Dim resourceDictionary As ResourceDictionary = ThemeManager.GetResourceSet(theme, ThemeManager.AllGroupingsLiteral)
1   2: If Not resourceDictionary Is Nothing Then
1   3:    Application.Current.Resources.MergedDictionaries.Add(resourceDictionary)
1   4: End If

This causes an error in the ItemsControlRegionAdapter as WPF seams to redo the region adapters and you get a ItemsControlHasItemsSourceException. You need to change the code to the following (notice the commented out areas):

1   1: /// <summary>
1   2: /// Adapts an <see cref="ItemsControl"/> to an <see cref="IRegion"/>.
1   3: /// </summary>
1   4: /// <param name="region">The new region being used.</param>
1   5: /// <param name="regionTarget">The object to adapt.</param>
1   6: protected override void Adapt(IRegion region, ItemsControl regionTarget)
1   7: {  //Modified by Martin Hinshelwood to allow resource file changes...
1   8:     //if (regionTarget.ItemsSource != null || (BindingOperations.GetBinding(regionTarget, ItemsControl.ItemsSourceProperty) != null))
1   9:     //    throw new InvalidOperationException(Resources.ItemsControlHasItemsSourceException);
1  10:
1  11:     //If control has child items, move them to the region and then bind control to region. Can't set ItemsSource if child items exist.
1  12:     if (regionTarget.Items.Count > 0)
1  13:     {
1  14:         foreach (object childItem in regionTarget.Items)
1  15:         {
1  16:             region.Add(childItem);
1  17:         }
1  18:         //Control must be empty before setting ItemsSource
1  19:         regionTarget.ItemsSource = null;
1  20:         //regionTarget.Items.Clear();
1  21:     }
1  22:     regionTarget.ItemsSource = region.Views;
1  23: }

You will notice that I had to comment out the exception for existing controls as well as the Items.Clear (which is replaced by setting the ItemsSource to nothing). This solves the problem I I have not noticed any adverse reactions.

The second problem occurs when you do you second set of the theme. at this point you need to remove the existing theme:

1   1: If Not m_CurrentTheme Is Nothing Then
1   2:       Application.Current.Resources.MergedDictionaries.Remove(m_CurrentTheme)
1   3: End If

When this happens the region management is redone and you get a further RegionNameExistsException from the RegionManager. Then can be solved by changing the code in the AttachNewRegion method:

1   1: /// <summary>
1   2: /// Attaches a region to an object and adds it to the region manager.
1   3: /// </summary>
1   4: /// <param name="regionTarget">The object to adapt. This is typically a container (i.e a control).</param>
1   5: /// <param name="regionName">The name of the region to register.</param>
1   6: /// <exception cref="ArgumentException">When regions collection already has a region registered using <paramref name="regionName"/>.</exception>
1   7: public void AttachNewRegion(object regionTarget, string regionName)
1   8: {  //Modified by Martin Hinshelwood to allow resource file changes...
1   9:     if (Regions.ContainsKey(regionName))
1  10:         return; //throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, Resources.RegionNameExistsException, regionName));
1  11:
1  12:     IRegionAdapter regionAdapter = regionAdapterMappings.GetMapping(regionTarget.GetType());
1  13:     IRegion region = regionAdapter.Initialize(regionTarget);
1  14:
1  15:     Regions.Add(regionName, region);
1  16: }

So instead of bombing out when you try to add a region of the same name, it will just ignore it. Not ideal, but necessary.

Technorati Tags: WPF ALM TFS Custom WIT

Smart Classifications

Each classification [Concepts, Categories, & Tags] was assigned using AI-powered semantic analysis and scored across relevance, depth, and alignment. Final decisions? Still human. Always traceable. Hover to see how it applies.

Subscribe

Connect with Martin Hinshelwood

If you've made it this far, it's worth connecting with our principal consultant and coach, Martin Hinshelwood, for a 30-minute 'ask me anything' call.

Our Happy Clients​

We partner with businesses across diverse industries, including finance, insurance, healthcare, pharmaceuticals, technology, engineering, transportation, hospitality, entertainment, legal, government, and military sectors.​

Capita Secure Information Solutions Ltd Logo

Capita Secure Information Solutions Ltd

NIT A/S

DFDS Logo

DFDS

Milliman Logo

Milliman

Graham & Brown Logo

Graham & Brown

Boeing Logo

Boeing

Freadom Logo

Freadom

Slaughter and May Logo

Slaughter and May

Akaditi Logo

Akaditi

Workday Logo

Workday

Cognizant Microsoft Business Group (MBG) Logo

Cognizant Microsoft Business Group (MBG)

Higher Education Statistics Agency Logo

Higher Education Statistics Agency

Microsoft Logo

Microsoft

Jack Links Logo

Jack Links

Healthgrades Logo

Healthgrades

Boxit Document Solutions

YearUp.org Logo

YearUp.org

Philips Logo

Philips

New Hampshire Supreme Court Logo

New Hampshire Supreme Court

Washington Department of Enterprise Services Logo

Washington Department of Enterprise Services

Royal Air Force Logo

Royal Air Force

Ghana Police Service Logo

Ghana Police Service

Department of Work and Pensions (UK) Logo

Department of Work and Pensions (UK)

Washington Department of Transport Logo

Washington Department of Transport

Xceptor - Process and Data Automation Logo

Xceptor - Process and Data Automation

Healthgrades Logo

Healthgrades

Qualco Logo

Qualco

Bistech Logo

Bistech

Brandes Investment Partners L.P. Logo

Brandes Investment Partners L.P.

Akaditi Logo

Akaditi