Built-in converter
BooleanToVisibilityConverter
BooleanToVisibilityConverter is located in System.Windows.Controls.
|
<Window>
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</Window.Resources>
<TextBlock Visibility="{Binding IsVisible, Converter={StaticResource BooleanToVisibilityConverter}}" />
|
ValueConverter
|
<UserControl xmlns:conversion="clr-namespace:MyNamespace.Conversion">
<UserControl.Resources>
<conversion:EnumerableToVisibilityConverter x:Key="EnumerableToVisibilityConverter" />
</UserControl.Resources>
<TextBlock Text="{Binding MyProperty, Converter={StaticResource EnumerableToVisibilityConverter}}" />
|
|
namespace MyNamespace.Conversion
{
// L'attribut ValueConversion est optionel,
// il permet de spécifier le type d'entrée et le type de sortie
[ValueConversion(typeof(IEnumerable), typeof(Visibility))]
public class ListToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is IEnumerable itemCollection)
{
return itemCollection.Cast<object>().Any() ? Visibility.Visible : Visibility.Collapsed;
}
return Visibility.Collapsed;
}
// Conversion inverse non-implémentée
public object ConvertBack(object value, Type targetTypes, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
throw new NotImplementedException();
return null;
}
}
}
|
|
<TextBlock Visibility="{Binding MyProperty, Converter={StaticResource BooleanToVisibilityConverter}, ConverterParameter=Inverted}" />
|
BooleanToVisibilityConverter.cs
|
[ValueConversion(typeof(bool), typeof(Visibility))]
public sealed class BooleanToVisibilityConverter : IValueConverter
{
public object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool visibleFlag;
if (value is bool b)
{
visibleFlag = b;
}
else
{
return DependencyProperty.UnsetValue;
}
if (parameter != null && Enum.TryParse(parameter.ToString(), true, out Parameters invert) && invert == Parameters.Inverted)
{
visibleFlag = !visibleFlag;
}
return visibleFlag ? Visibility.Visible : Visibility.Collapsed;
}
public object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
private enum Parameters
{
Normal,
Inverted
}
|
MultiValueConverter
|
<UserControl xmlns:my="clr-namespace:CurrentNameSpace">
<UserControl.Resources>
<my:MyConverter x:Key="myConverter"/>
</UserControl.Resources>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource ResourceKey=myConverter}">
<Binding ... />
<Binding ... />
</MultiBinding>
</TextBlock.Text>
|
|
namespace CurrentNameSpace
{
// L'attribut ValueConversion n'est pas utilisé ici car il n'est pas adapté
// au MultiValueConverter : il ne peut définir qu'un seul type d'entrée
public class MyConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var binding1Value = (int)values[0];
var binding2Value = (string)values[1];
...
return ""; // un string
}
// Conversion inverse non-implémentée
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
|