This isn't MahApps.Metro-specific, but that happens to be what I'm using. I have a set of ViewModels that have a string
property representing which icon to use from a resource XAML file.
public class CommandViewModel : ViewModel
{
public CommandViewModel(string displayName, ICommand command, string icon)
{
if (command == null)
throw new ArgumentNullException("command");
DisplayName = displayName;
Command = command;
Icon = icon;
}
public ICommand Command { get; private set; }
public string Icon { get; set; }
}
Icon
would end up being something like "appbar_add" from MahApps.Metro.Resources. These are defined in an Icons.xaml file.
How do I write this up in my ItemTemplate
such that the correct resource shows. I'm either getting errors on execution (not at edit/build) or I'm getting no icons at all.
The "no icon" XAML looks like this:
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Rectangle Width="20" Height="20">
<Rectangle.Fill>
<VisualBrush Visual="{DynamicResource {Binding Path=Icon}}" />
</Rectangle.Fill>
</Rectangle>
<TextBlock Margin="15,6">
<Hyperlink Command="{Binding Path=Command}">
<TextBlock Text="{Binding Path=DisplayName}" />
</Hyperlink>
</TextBlock>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
My attempts that caused errors were using StaticResource
, which I think is fundamentally incorrect.
How should I be referencing the Icon
property as the name of the resource I want?
Edit
More code was requested, so here's an example of what does work:
<Rectangle Width="20" Height="20">
<Rectangle.Fill>
<VisualBrush Visual="{StaticResource appbar_add}" />
</Rectangle.Fill>
</Rectangle>
What I need to do is let "appbar_add" be a value from a property on my ViewModel -- the Icon
property above.
The resources are in a ResourceDictionary in a separate file (Icon.xaml) and look like the following:
<Canvas x:Key="appbar_add" Width="76" Height="76" Clip="F1 M 0,0L 76,0L 76,76L 0,76L 0,0">
<Path Width="38" Height="38" Canvas.Left="19" Canvas.Top="19" Stretch="Fill" Fill="{DynamicResource BlackBrush}" Data="F1 M 35,19L 41,19L 41,35L 57,35L 57,41L 41,41L 41,57L 35,57L 35,41L 19,41L 19,35L 35,35L 35,19 Z "/>
</Canvas>