Posted By

davidsi on 03/17/10


Tagged

c WPF xaml Silverlight


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

dbuksbaum


Value Converters for WPF and Silverlight


 / Published in: C#
 

Value converters allow ease of converting code-behind values to dependency property values

  1. /** XAML code
  2.  
  3.   <src:VisibilityFromBoolConverter x:Key="DefaultVisibleConverter" /> <!-- src is the app namespace, declared in header -->
  4.  
  5.   <Border Visibility="{Binding Selected, Converter={StaticResource DefaultVisibleConverter}}" Background="White" > <!-- usage of converter -->
  6.  
  7. **/
  8.  
  9. using System;
  10. using System.Globalization; // CultureInfo
  11. using System.Windows.Data; // IValueConverter
  12.  
  13. namespace mypcforsilvernet
  14. {
  15. public class VisibilityFromBoolConverter : IValueConverter
  16. {
  17. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  18. {
  19. if (value == null)
  20. return Visibility.Visible;
  21.  
  22. bool visibility = (bool)value;
  23.  
  24. return visibility ? Visibility.Visible : Visibility.Collapsed;
  25. }
  26.  
  27. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  28. {
  29. Visibility visibility = (Visibility)value;
  30.  
  31. return (visibility == Visibility.Visible);
  32. }
  33. }
  34. }

Report this snippet  

You need to login to post a comment.