WPF: how to use 2 converters in 1 binding?

2019-03-08 13:53发布

I have a control that I want to show/hide, depending on the value of a boolean.

I have a NegatedBooleanConverter (switches true to false and vice versa) and I need to run this converter first. I have a BooleanToVisibilityConverter and I need to run this converter after the NegatedBoolConverter.

How can I fix this problem? I want to do this in XAML.

edit: this is a possible solution.

That doesn't seem to work. It first converts the value with the seperate converters and then does something with the converted values.

What I need is:

  • Convert the value with the first converter (this gives convertedValue).
  • Convert convertedValue with the second converter and it's this result that I need.

9条回答
Melony?
2楼-- · 2019-03-08 14:05

What we do in our project is make a regular BooleanToVisibilityConverter, said converter takes one parameter (anything at all, a string, an int, bool, whatever). If the parameter is set it inverts the result, if not, it spits out the regular result.

public class BooleanToVisibilityConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool? isVisible = value as bool?;
        if (parameter != null && isVisible.HasValue)
            isVisible = !isVisible;
        if (isVisible.HasValue && isVisible.Value == true)
            return Visibility.Visible;
        else
            return Visibility.Collapsed;
    }

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

    #endregion
}
查看更多
成全新的幸福
3楼-- · 2019-03-08 14:05

To address this specific problem, instead of using two converters your could write your own BoolToVisibilityConverter that uses the ConverterParameter (as a bool) to determine whether or not to negate the original Boolean.

查看更多
时光不老,我们不散
4楼-- · 2019-03-08 14:08

To answer my own question again: I have been using this solution for years now: http://www.codeproject.com/Articles/15061/Piping-Value-Converters-in-WPF

It makes a new converter of 2 existing converters, calling the first one first, and then the second one etc etc.

I'm pretty pleased with this solution.

查看更多
Viruses.
5楼-- · 2019-03-08 14:09

Expanding on Natrium's great answer...

XAML

<conv:ConverterChain x:Key="convBoolToInverseToVisibility">
    <conv:BoolToInverseConverter />
    <BooleanToVisibilityConverter />
</conv:ConverterChain>

Class

/// <summary>Represents a chain of <see cref="IValueConverter"/>s to be executed in succession.</summary>
[ContentProperty("Converters")]
[ContentWrapper(typeof(ValueConverterCollection))]
public class ConverterChain : IValueConverter
{
    private readonly ValueConverterCollection _converters= new ValueConverterCollection();

    /// <summary>Gets the converters to execute.</summary>
    public ValueConverterCollection Converters
    {
        get { return _converters; }
    }

    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return Converters
            .Aggregate(value, (current, converter) => converter.Convert(current, targetType, parameter, culture));
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return Converters
            .Reverse()
            .Aggregate(value, (current, converter) => converter.Convert(current, targetType, parameter, culture));
    }

    #endregion
}

/// <summary>Represents a collection of <see cref="IValueConverter"/>s.</summary>
public sealed class ValueConverterCollection : Collection<IValueConverter> { }
查看更多
劫难
6楼-- · 2019-03-08 14:10

This is what I did:

public class CombiningConverter : IValueConverter
    {
        public IValueConverter Converter1 { get; set; }
        public IValueConverter Converter2 { get; set; }

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            object convertedValue = Converter1.Convert(value, targetType, parameter, culture);
            return Converter2.Convert(convertedValue, targetType, parameter, culture);
        }

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

        #endregion
    }

and I call it like this:

<converters:CombiningConverter x:Key="negatedBoolToVisibilityConverter" Converter1="{StaticResource NegatedBooleanConverter}" Converter2="{StaticResource BoolToVisibilityConverter}" />

A MultiValueConverter might also be possible I think. Maybe I'll try that later.

查看更多
一夜七次
7楼-- · 2019-03-08 14:10

I think you may want to use a Multiconverter here instead of two separate converters. You should be able to reuse the logic from your existing converters. Check out this discussion for a start.

查看更多
登录 后发表回答