I have a Silverlight controls assembly, called "MySilverlightControls". Several folders down into that assembly I have a class which extends a grid column from a third party vendor, let's call it "MyImageColumn.cs".
I have also created a resource dictionary called Generic.xaml
, this is situated in the Themes
folder of the assembly. In that resource dictionary i have defined a ControlTemplate called MyImageColumnTemplate:
<ControlTemplate x:Name="MyImageColumnTemplate" >
<Grid Margin="8,8,4,4" MaxHeight="32" MaxWidth="32">
<Grid.Resources>
<localGrid:StatusColumnImageConverter x:Key="ImageContentConverter"/>
</Grid.Resources>
<Border Margin="5,5,0,0" Background="Black" Opacity="0.15" CornerRadius="5" />
<Border Background="#FF6E6E6E" CornerRadius="4,4,4,4" Padding="4" Margin="0,0,5,5">
<Border Background="White" CornerRadius="2,2,2,2" Padding="3">
<Image Source="{Binding EditValue, Converter={StaticResource ImageContentConverter}}" Stretch="Uniform"/>
</Border>
</Border>
</Grid>
</ControlTemplate>
My question is: from MyImageColumn, how can I programmatically reference/load this control template so I can assign it to a property on the column? I would expect to be using a syntax similar to this:
ControlTemplate ct = (ControlTemplate)Application.Current.Resources["MyImageColumnTemplate"];
but this always returns null. When I load the assembly up in Reflector, I see that the Generic.xaml
file is there, the name of the resource is MySilverlightControls.g.resources
, and the path within that is themes/generic.xaml
.
How exactly can I get to the individual items in this resource dictionary?
Got it solved.
I needed to:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With