wpf - How do I use a ViewModel variable for a DynamicResource? -
this isn't mahapps.metro-specific, happens i'm using. have set of viewmodels have string
property representing icon use 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
end being "appbar_add" mahapps.metro.resources. these defined in icons.xaml file.
how write in itemtemplate
such correct resource shows. i'm either getting errors on execution (not @ edit/build) or i'm getting no icons @ all.
the "no icon" xaml looks 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 caused errors using staticresource
, think fundamentally incorrect.
how should referencing icon
property name of resource want?
edit
more code requested, here's example of does work:
<rectangle width="20" height="20"> <rectangle.fill> <visualbrush visual="{staticresource appbar_add}" /> </rectangle.fill> </rectangle>
what need let "appbar_add" value property on viewmodel -- icon
property above.
the resources in resourcedictionary in separate file (icon.xaml) , 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>
you can use converter work. refer below code. have 2 style make + symbol in red color or black color in icon.xaml.
<resourcedictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <canvas x:key="appbar_add_black" 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="black" 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> <canvas x:key="appbar_add_red" 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="red" 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>
refer view code. viewmodel
public partial class mainwindow : window { public mainwindow() { initializecomponent(); this.datacontext = new commandviewmodel("red"); } } public class commandviewmodel :inotifypropertychanged { public event propertychangedeventhandler propertychanged; private void onpropertychanged(string propname) { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(propname)); } } public commandviewmodel(string icon) { icon = icon; } private string icon; public string icon { { return icon; } set { icon = value; } } } class iconconverter : ivalueconverter { public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { string str = (string)value; resourcedictionary myresourcedictionary = new resourcedictionary(); myresourcedictionary.source = new uri("icon.xaml", urikind.relative); if (str.equals("black")) { return myresourcedictionary["appbar_add_black"]; } else { return myresourcedictionary["appbar_add_red"]; } } public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { throw new notimplementedexception(); } }
Comments
Post a Comment