Posted By

bokkers on 08/10/10


Tagged

c xaml MVVM


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

Vordreller
Tyster


Binding Visibility property (View) with Bool property (ViewModel)


 / Published in: C#
 

  1. <!-- XAML VisibilityConverter Usage -->
  2.  
  3.  
  4. <Window.Resources>
  5. <vw:VisibilityConverter x:Key="VisibilityConverter" />
  6. </Window.Resources>
  7.  
  8. <Grid Visibility="{Binding MyBoolean, Converter={StaticResource VisibilityConverter}}">
  9. ...
  10. </Grid>
  11.  
  12.  
  13. // C# VisibilityConverter implementation
  14.  
  15. public class VisibilityConverter : IValueConverter
  16. {
  17. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  18. {
  19. Visibility rv = Visibility.Visible;
  20. try
  21. {
  22. var x = bool.Parse(value.ToString());
  23. if (x)
  24. {
  25. rv = Visibility.Visible;
  26. }
  27. else
  28. {
  29. rv = Visibility.Collapsed;
  30. }
  31. }
  32. catch (Exception)
  33. {
  34. }
  35. return rv;
  36. }
  37.  
  38. public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  39. {
  40. return value;
  41. }
  42. }

Report this snippet  

You need to login to post a comment.