xaml - WPF one trigger for multiple values -


i'm trying create single trigger trigger on multiple possible options. want set background green when options either "reviewed" or "completed". want second trigger change background yellow when "pending" or "yellow".

this answer pointed me towards it, incomplete , couldn't make sense of it: https://stackoverflow.com/a/4660030/526704

here's have now:

<style x:key="statuscelltextblock" targettype="textblock">     <style.triggers>         <trigger property="text" value="reviewed">             <setter property="background" value="green"/>         </trigger>         <trigger property="text" value="completed">             <setter property="background" value="green"/>         </trigger>         <trigger property="text" value="pending">             <setter property="background" value="yellow"/>         </trigger>         <trigger property="text" value="pending review">             <setter property="background" value="yellow"/>         </trigger>     </style.triggers> </style> 

here's kind of thing i'm looking for: (some way specify multiple values of property trigger same setters. have many more of these i'd condense without repeating same trigger many times)

<style x:key="statuscelltextblock" targettype="textblock">     <style.triggers>         <trigger property="text" value="reviewed" value2="completed">             <setter property="background" value="green"/>         </trigger>         <trigger property="text" value="pending" value2="pending review">             <setter property="background" value="yellow"/>         </trigger>     </style.triggers> </style> 

you can create custom ivalueconverter convert string solidcolorbrush

public class texttobackgroundconverter : ivalueconverter {     public object convert(object value, type targettype, object parameter, cultureinfo culture)     {         switch ((string)value)         {             case "reviewed":             case "completed":                 return new solidcolorbrush(colors.green);             case "pending":             case "pending review":                 return new solidcolorbrush(colors.yellow);         }         return dependencyproperty.unsetvalue;     }     public object convertback(object value, type targettype, object parameter, cultureinfo culture)     {         throw new notimplementedexception();     } } 

and don't need trigger. can use binding converter

<style x:key="statuscelltextblock" targettype="textblock">     <setter          property="background"          value="{binding relativesource={relativesource self}, path=text, converter={staticresource texttobackgroundconverter}}"/> </style> 

where texttobackgroundconverter defined somewhere in resources as

<window.resources>     <local:texttobackgroundconverter x:key="texttobackgroundconverter"/> </window.resources> 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -