« Converter » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
Ligne 102 : Ligne 102 :
=== [https://code.4noobz.net/booleantovisibilityconverter Inverted convertion with ConverterParameter] ===
=== [https://code.4noobz.net/booleantovisibilityconverter Inverted convertion with ConverterParameter] ===
<kode lang='xaml'>
<kode lang='xaml'>
<TextBlock Visibility="{Binding MyProperty, Converter={StaticResource BooleanToVisibilityConverter}, ConverterParameter=Inverted}" />
<TextBlock Visibility="{Binding MyList, Converter={StaticResource ListToVisibilityConverter}, ConverterParameter=Inverted}" />
</kode>
</kode>


<filebox fn='BooleanToVisibilityConverter.cs'>
<filebox fn='ListToVisibilityConverter.cs'>
[ValueConversion(typeof(bool), typeof(Visibility))]
[ValueConversion(typeof(IEnumerable), typeof(Visibility))]
public sealed class BooleanToVisibilityConverter : IValueConverter
public class ListToVisibilityConverter : IValueConverter
{
{
     public object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
     {
     {
         bool visibleFlag;
         var visibleFlag = false;
         if (value is bool b)
         if (value is IEnumerable itemCollection)
         {
         {
             visibleFlag = b;
             visibleFlag = itemCollection.Cast<object>().Any();
         }
         }
         else
         else
Ligne 129 : Ligne 129 :
     }
     }


     public object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    // Conversion inverse non-implémentée
     public object ConvertBack(object value, Type targetTypes, object parameter, CultureInfo culture)
     {
     {
         throw new NotSupportedException();
         throw new NotImplementedException();
     }
     }



Version du 17 avril 2020 à 23:23

Built-in converter

BooleanToVisibilityConverter

BooleanToVisibilityConverter is located in System.Windows.Controls.

Xaml.svg
<Window>
    <Window.Resources>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
    </Window.Resources>

    <TextBlock Visibility="{Binding IsVisible, Converter={StaticResource BooleanToVisibilityConverter}}" />

ValueConverter

Xaml.svg
<UserControl xmlns:conversion="clr-namespace:MyNamespace.Conversion">

    <UserControl.Resources>
        <conversion:ListToVisibilityConverter x:Key="ListToVisibilityConverter" />
    </UserControl.Resources>

    <TextBlock Visibility="{Binding MyList, Converter={StaticResource ListToVisibilityConverter}}" />
Csharp.svg
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 NotImplementedException();
            throw new NotSupportedException();
            return null;
        }
    }
}

Inverted convertion

Inverted convertion with IsInverted property

Xaml.svg
<UserControl xmlns:conversion="clr-namespace:MyNamespace.Conversion">

    <UserControl.Resources>
        <conversion:ListToVisibilityConverter x:Key="EmptyListToCollapsedConverter" />
        <conversion:ListToVisibilityConverter x:Key="EmptyListToVisibleConverter"
                                              IsInverted="true" />
    </UserControl.Resources>

    <TextBlock Visibility="{Binding MyList, Converter={StaticResource EmptyListToCollapsedConverter}}" />
    <TextBlock Visibility="{Binding MyList, Converter={StaticResource EmptyListToVisibleConverter}}" />
Cs.svg
namespace MyNamespace.Conversion
{
    [ValueConversion(typeof(IEnumerable), typeof(Visibility))]
    public class ListToVisibilityConverter : IValueConverter
    {
        public bool IsInverted { get; set; }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var visibleFlag = false;
            if (value is IEnumerable itemCollection)
            {
                visibleFlag = itemCollection.Cast<object>().Any();
            }

            if (this.IsInverted)
            {
                visibleFlag = !visibleFlag;
            }

            return visibleFlag ? Visibility.Visible : Visibility.Collapsed;
        }

        // Conversion inverse non-implémentée
        public object ConvertBack(object value, Type targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

Inverted convertion with ConverterParameter

Xaml.svg
<TextBlock Visibility="{Binding MyList, Converter={StaticResource ListToVisibilityConverter}, ConverterParameter=Inverted}" />
ListToVisibilityConverter.cs
[ValueConversion(typeof(IEnumerable), typeof(Visibility))]
public class ListToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var visibleFlag = false;
        if (value is IEnumerable itemCollection)
        {
            visibleFlag = itemCollection.Cast<object>().Any();
        }
        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;
    }

    // Conversion inverse non-implémentée
    public object ConvertBack(object value, Type targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    private enum Parameters
    {
        Normal,
        Inverted
    }

MultiValueConverter

Xaml.svg
<UserControl xmlns:conversion="clr-namespace:MyNamespace.Conversion">

    <UserControl.Resources>
        <conversion:TakeItemsConverter x:Key="TakeItemsConverter"/>
    </UserControl.Resources>

    <ItemsControl>
        <ItemsControl.ItemsSource>
            <MultiBinding Converter="{StaticResource TakeItemsConverter}">
                <Binding Path="Items" />
                <Binding Path="MaxNumberOfItemsToDisplay" />
            </MultiBinding>
        </ItemsControl.ItemsSource>
    </ItemsControl>
Csharp.svg
namespace MyNamespace.Conversion
{
    // 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 TakeItemsConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            // expect 2 values, an IEnumerable and an int
            if (values != null
                && values.Length == 2
                && values[0] is IEnumerable itemCollection
                && values[1] != null
                && int.TryParse(values[1].ToString(), NumberStyles.Integer, culture, out var numberOfItemsToTake))
            {
                return itemCollection.Cast<object>().Take(numberOfItemsToTake);
            }

            return DependencyProperty.UnsetValue;
        }

        // Conversion inverse non-implémentée
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}