Access ResourceDictionary items programmatically

Got it solved.

I needed to:

  • load my resource dictionary
  • merge it with the application’s resources
  • load my control template from the application resource

As part of loading the resource dictionary, i also had to register the pack URI scheme. I then had to deal with some crazy COM based exceptions due to slight errors with my xaml. I also had to move my xaml into a separate resource dictionary file, trying to do it through generic.xaml kept throwing errors (even though the xaml was faultless and could be loaded fine using the newly created resource dictionary file). So, simplifying it down, this was the code:

if (!UriParser.IsKnownScheme("pack"))
    UriParser.Register(new GenericUriParser(GenericUriParserOptions.GenericAuthority), "pack", -1);

ResourceDictionary dict = new ResourceDictionary();
Uri uri = new Uri("/MySilverlightControls;component/themes/Dictionary1.xaml", UriKind.Relative);
dict.Source = uri;
Application.Current.Resources.MergedDictionaries.Add(dict);
ControlTemplate ct = (ControlTemplate)Application.Current.Resources["MyImageColumnTemplate"];

I have posted the full details for this solution in this blog post.

Leave a Comment