Reputation: 45
Right now in xaml I have sys:String values defined as follows. Is it possible to bind the values from a resource dictionary. Lets say instead of Bmp (harcoded text) I want to say {StaticResource Bmp}. The Bmp value comes from some resource. Please help.
xmlns:sys="clr-namespace:System;assembly=mscorlib"
<x:Array
Type="{x:Type sys:String}"
x:Key="ImageFormatsArray">
<sys:String>Bmp</sys:String>
<sys:String>Png</sys:String>
<sys:String>Jpg</sys:String>
<sys:String>Tif</sys:String>
<sys:String>Gif</sys:String>
</x:Array>
Upvotes: 0
Views: 2564
Reputation: 25623
Yes, you can. Just replace the relevant <sys:String>
entry with a StaticResource
reference:
<sys:String x:Key="BitmapFormat">Bmp</sys:String>
<x:Array x:Key="ImageFormatsArray"
Type="{x:Type sys:String}">
<StaticResource ResourceKey="BitmapFormat" />
<sys:String>Png</sys:String>
<sys:String>Jpg</sys:String>
<sys:String>Tif</sys:String>
<sys:String>Gif</sys:String>
</x:Array>
However, this only works as a static resource; you can't use a binding or a dynamic resource. That means the resource must be in scope at the time the Xaml is parsed and the array is created.
You could also reference a named constant using x:Static
:
public static class ImageFormats
{
public const string Bitmap = "Bmp";
}
<x:Array x:Key="ImageFormatsArray"
Type="{x:Type sys:String}">
<x:Static Member="local:ImageFormats.Bitmap" />
<!-- ... more formats -->
</x:Array>
Of course, if you're going to go that far, you may as well just hard-code the entire list:
public static class ImageFormats
{
public const string Bitmap = "Bmp";
public const string Png = "Png";
public const string Jpeg = "Jpg";
public const string Tiff = "Tif";
public const string Gif = "Gif";
public static readonly IReadOnlyList<string> AllFormats =
new[] { Bitmap, Png, Jpeg, Tiff, Gif };
}
And then use x:Static
to access the list of formats:
<ComboBox ItemsSource="{x:Static local:ImageFormats.AllFormats}" />
Upvotes: 1