Guest User

Untitled

a guest
Mar 23rd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. enum StateValue { Player, Wall, Box }
  2. enum StateColor { Colors.Red, Colors.Grey, Colors.Brown }
  3.  
  4. <Button Content="Player" Background="{Binding Source=...?}" />
  5.  
  6. public Dictionary<StateValue, Color> stateValueColor =
  7. new Dictionary<ElementState, Color>()
  8. {
  9. { StateValue.Player, Colors.Red },
  10. { StateValue.Wall, Colors.Grey },
  11. { StateValue.Box, Colors.Brown }
  12. };
  13.  
  14. [ValueConversion(typeof(StateValue), typeof(Color))]
  15. public class StateValueColorConverter : IValueConverter
  16. {
  17. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  18. {
  19. if(!(value is StateValue))
  20. throw new ArgumentException("value not of type StateValue");
  21. StateValue sv = (StateValue)value;
  22. //sanity checks
  23. switch (sv)
  24. {
  25. case StateValue.Player:
  26. return Colors.Red;
  27. //etc
  28. }
  29. }
  30.  
  31. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  32. {
  33. Color c = (value as Color);
  34. if(c == Colors.Red)
  35. return StateValue.Player;
  36. //etc
  37. }
  38. }
  39.  
  40. <Button
  41. Content="Player"
  42. Background="{Binding Source=StateValue,
  43. Converter={StaticResource stateValueColorConverter}}" />
Add Comment
Please, Sign In to add comment