duminică, 13 iunie 2010

[WPF][9][1]Converters

[WPF][9][1]Converters


Value Converters

  • Value converters can morph the source value into a completely different target value
  • A value converter is an implementation of the IValueConverter interface, which contains two methods: Convert and ConvertBack
  • You can define your own converter. It has to implement IValueConverter
    • BooleanToVisibilityConverter

BoolToVisibilityConverter 

    [ValueConversion(typeof(bool), typeof(Visibility))]
    public class BoolToVisibilityConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ((bool)value) ? Visibility.Visible : Visibility.Hidden;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

<Convertors:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
...
<Grid Visibility="{TemplateBinding IsRunning, Converter={StaticResource BoolToVisibilityConverter}}">

StringToRowSpanConverter 


      /// <summary>
    /// If the string provided is null or empty, it returns 1, else 2
    /// </summary>
    [ValueConversion(typeof(string), typeof(int))]
    public class StringToRowSpanConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (value == null || value.ToString() == string.Empty) ? 2 : 1;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

StringToVisibilityConverter 

    [ValueConversion(typeof(string), typeof(Visibility))]
    public class StringToVisibilityConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (value == null || value.ToString() == string.Empty) ? Visibility.Hidden : Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

<Convertors:StringToVisibilityConverter x:Key="StringToVisibilityConverter"/>
...
<Label Grid.Column="1" Grid.Row="1"
               VerticalContentAlignment="Center" HorizontalAlignment="Center" 
               Margin="1"
               Visibility="{Binding DetailedMessage, Converter={StaticResource StringToVisibilityConverter}}"
               ToolTip="{Binding DetailedMessage}" FontSize="9">
</Label>

DateTimeToDateConverter 


    public class DateTimeToDateConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ((DateTime)value).DayOfWeek;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }

ItemCountToDescriptionConverter 


    public class ItemCountToDescriptionConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // Let Parse throw an exception if the input is bad
            int num = int.Parse(value.ToString());
            return num + (num == 1 ? " item" : " items");
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }

<Window.Resources>
   <ResourceDictionary>
     <converter:ItemCountToDescriptionConverter x:Key="m_converter"/>
...

<TextBlock Grid.Row="0" Margin="10"
                Text="{Binding Path=Count, Converter={StaticResource m_converter}}" />
...

HexConverter 


public class HexConverter : IValueConverter 
{
  public object Convert( object value, Type targetType, object parameter, CultureInfo culture)
  {
   // Convert to base 16
   return ((int)value).ToString("x");
  }
  public object ConvertBack(object value, Type targetType, , object parameter, CultureInfo culture) 
  {
    // Convert from base 16
    return int.Parse((string)value, System.Globalization.NumberStyles.HexNumber);
  }
}

<Convertors:HexConverter r x:Key="hexConverter"/>
<TextBox Text="{Binding Path=StatValue, Converter={StaticResource hexConverter}}" />

Niciun comentariu:

Trimiteți un comentariu