Namespace | Assembly |
---|---|
System.Windows.Data |
PresentationFramework.dll |
Value converters provides a way to apply custom logic to a binding.
When source object type and target object type are different, value converters act like middlemen.
Converter class must implement IValueConverter interface, which consists of two methods, Convert()
and ConvertBack()
.
Convert method gets called when source updates target object.
public object Convert (object value, Type targetType, object parameter, CultureInfo culture);
// parameters:
// value: The value produced by the binding source.
// targetType: The type of the binding target property.
// parameter: The converter parameter to use.
// culture: The culture to use in the converter.
// return:
// A converted value. If the method returns null, the valid null value is used.
ConvertBack method gets called when target updates source object.
public object ConvertBack (object value, Type targetType, object parameter, CultureInfo culture);
// parameters:
// value: The value that is produced by the binding target.
// targetType: The type to convert to.
// parameter: The converter parameter to use.
// culture: The culture to use in the converter.
// return:
// A converted value. If the method returns null, the valid null value is used.
π Download
public class BooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value.Equals(true) ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
xmlns:cvt="clr-namespace:IValueConverterSample.Converters"
<cvt:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
<Style TargetType="{x:Type TextBlock}" x:Key="TXB.HELLO">
<Setter Property="Grid.Row" Value="0"/>
<Setter Property="Text" Value="Hello!"/>
<Setter Property="Foreground" Value="#EFE6D4"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="FontSize" Value="18"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="Margin" Value="100 22 0 0"/>
<Setter Property="Visibility" Value="{Binding ElementName=tgl, Path=IsChecked,
Converter={StaticResource BooleanToVisibilityConverter}}"/>
</Style>
'IsChecked' = true | 'IsChecked' = false |
---|---|
public class StringFormatConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return string.Format("{0:N0}%", value);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
xmlns:cvt="clr-namespace:IValueConverterSample.Converters"
<cvt:StringFormatConverter x:Key="StringFormatConverter"/>
<Style TargetType="{x:Type TextBlock}" x:Key="IN.CONTENT">
<Setter Property="Grid.Column" Value="1"/>
<Setter Property="Foreground" Value="#9B9688"/>
<Setter Property="FontSize" Value="13"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="10 20 0 0"/>
<Setter Property="Text" Value="{Binding ElementName=slider2, Path=Value,
Converter={StaticResource StringFormatConverter}}"/>
</Style>
public class MultiValueBooleanConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return ((bool)values[0] == true && !string.IsNullOrWhiteSpace(values[1]?.ToString()));
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
xmlns:cvt="clr-namespace:IValueConverterSample.Converters"
<cvt:MultiValueBooleanConverter x:Key="MultiValueBooleanConverter"/>
<Style TargetType="{x:Type ToggleButton}" x:Key="TGL.ACCEPT" BasedOn="{StaticResource TGL.BASE}">
...
<Setter Property="IsEnabled">
<Setter.Value>
<MultiBinding Converter="{StaticResource MultiValueBooleanConverter}">
<Binding ElementName="rdo1" Path="IsChecked"/>
<Binding ElementName="txt" Path="Text"/>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
'IsEnabled' = true | 'IsEnabled' = false |
---|---|
public class FileSizeToFormatConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string[] sizes = { "B", "KB", "MB", "GB", "TB" };
long len = 0;
if (value.ToString() != "")
{
len = long.Parse(value.ToString());
}
int order = 0;
while (len >= 1024 && order < sizes.Length - 1)
{
order++;
len = len / 1024;
}
// Adjust the format string to your preferences. For example "{0:0.#}{1}" would
// show a single decimal place, and no space.
return String.Format("({0:0.##} {1})", len, sizes[order]);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
xmlns:cvt="clr-namespace:IValueConverterSample.Converters"
<cvt:FileSizeToFormatConverter x:Key="FileSizeToFormatConverter"/>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding Name}"/>
<TextBlock Grid.Column="1" Text="{Binding Length, Converter={StaticResource FileSizeToFormatConverter}}"/>
</Grid>
π CODE PROJECT IValueConverter Example and Usage in WPF
π Microsoft Docs IValueConverter Interface
π WPF Tutorial Value conversion with IValueConverter