views:

528

answers:

6

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.
A: 

I've just created what I've called ReversedBooleanToVisibilityConverter to basically do what those 2 would do for you but in one step.

Alan Mendelevich
Yes, that would a possible solution, but I prefer a solution where I can reuse my current converters without having to reinvent the wheel. If that solution exists...
Natrium
A: 

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.

John Myczek
Yes, that would a possible solution, but I prefer a solution where I can reuse my current converters without having to reinvent the wheel. If that solution exists...
Natrium
A: 

Personally I would just make 1 single converter that does the full conversion. Unless you desperately need the converters (like the negation) in other places, it will be easier to maintain (imo) if the conversion is done once, in one place.

Alastair Pitts
Yes, that would a possible solution, but I prefer a solution where I can reuse my current converters without having to reinvent the wheel. If that solution exists...
Natrium
A: 

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.

Scott A. Lawrence
+1  A: 

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
}
Carlo
how to call this converter in xaml?
Natrium
+1  A: 

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.

Natrium