c# - Show alternating images for changing ViewModel states -
for different user interfaces want show image depending on state of viewmodel object.
for example:
i have database connection, if connected, want show green database image, if not connected want display red database image.
in viewmodel there bool
represents state.
possibilities are:
having 2 images in view (with converter inversebooleantovisibilityconverter red image), @ same place, showing 1 of them.
binding
image source
(but not want set in viewmodel)some sort of selector?
this state depending image can more of use, e.g. in treeview
itemimage
.
is there more clever way accomplish that?
i use valueconverter so:
public class booltoimageconverter: dependencyobject, ivalueconverter { public bitmapimage trueimage { { return (bitmapimage)getvalue(trueimageproperty); } set { setvalue(trueimageproperty, value); } } public static dependencyproperty trueimageproperty = dependencyproperty.register("trueimage", typeof(bitmapimage), typeof(booltoimageconverter), null); public bitmapimage falseimage { { return (bitmapimage)getvalue(falseimageproperty); } set { setvalue(falseimageproperty, value); } } public static dependencyproperty falseimageproperty = dependencyproperty.register("falseimage", typeof(bitmapimage), typeof(booltoimageconverter), null); public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { return (bool)value ? trueimage : falseimage; } public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { var img = (bitmapimage)value; return img.urisource.absoluteuri == trueimage.urisource.absoluteuri; } }
and in xaml
<my:booltoimageconverter x:key="convertername" falseimage="{staticresource falseimage}" trueimage="{staticresource trueimage}"/>
Comments
Post a Comment