wpf - Change DynamicResource from MVVM -


i trying bind dynamicresource value public property located in viewmodel , manipulate afterwards. static resource showing image. suggestions, thanks.

 <rectangle width="20" height="20">      <rectangle.fill>          <visualbrush stretch="fill" visual="{staticresource appbar_page_number1}" />      </rectangle.fill>  </rectangle> 

if have fixed number of resources , want convert lets enum resource can use binding converter. this:

public enum possiblevalue {     value1,     value2,     value3,     value4 } 

the converter like:

public class enumtovisualconverter : ivalueconverter {      public visual visual1 { get; set; }     public visual visual2 { get; set; }     public visual visual3 { get; set; }     public visual visual4 { get; set; }        public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture)     {         if (!(value possiblevalue))             return null; //or default visual          possiblevalue val = (possiblevalue)value;          switch (val)         {             case possiblevalue.value1:                 return visual1;             case possiblevalue.value2:                 return visual2;             case possiblevalue.value3:                 return visual3;             default:                 return visual4;         }     }      public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture)     {         throw new notimplementedexception();     } } 

and declare converter in resources section :

<l:enumtovisualconverter x:key="myconverter"         visual1="{staticresource appbar_page_number1}"         visual2="{staticresource appbar_page_number2}"         visual3="{staticresource appbar_page_number3}"         visual4="{staticresource appbar_page_number4}"/> 

now link rectangle property in vm (let's call property mightyproperty).

<rectangle width="20" height="20">     <rectangle.fill>         <visualbrush stretch="fill" visual="{binding mightyproperty, converter={staticresource myconverter}}" />     </rectangle.fill> </rectangle> 

in way each time mightyproperty change in view model converter find appropriate visual , send visual property of visualbrush.

of course mightyproperty don't have of enum type can int string or other type. have adapt code inside convert method match type.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -