2

I am trying to set up the visibility property on a control so that it is visible when the bound value is matches an arbitrary value.

I have set up my converter as a static resource

Applied the binding

<Button Content="Foo" Visibility="{Binding SelectedValue, Converter={StaticResource ValueToVisibilityConverter}, ConverterParameter='1,2'}" />

But am met with the error

Error 1 '{Binding SelectedValue, Converter={StaticResource ValueToVisibilityConverter}, ConverterParameter='1,2'}' cannot be used as a value for 'Visibility'. Numbers are not valid enumeration values.

My converter code is

public class ValueToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,System.Globalization.CultureInfo culture)
        {
            if (value == null || parameter == null || !(value is String))
                return Visibility.Collapsed;

            var currentValue = value.ToString();
            var matchStrings = parameter.ToString();
            var found = false;

            foreach (var state in matchStrings.Split(','))
            {
                found = (currentValue == state.Trim());

                if (found)
                    break;
            }

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

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

The error stops compile and feels like it is trying to be too clever and is ignoring my converter.

Have I applied it wrong or am otherwise ignorant of some process going on.

EDIT:

To get the converter as a static resource I have the below in my window definition

xmlns:myConverters="clr-namespace:<namespace>;assembly=<assemblyname>"

And this in my window resources, right along side the same code for other converters that work perfectly

<myConverters:ValueToVisibilityConverter x:Key="ValueToVisibilityConverter" />
Hugoagogo
  • 1,598
  • 16
  • 34

2 Answers2

3

This is the code that should work.

<Button Content="Foo" 
        Visibility="{Binding SelectedValue, 
                     Converter={StaticResource ValueToVisibilityConverter}, 
                     ConverterParameter=1|2}" />

Things that you need to do

  1. Values in ConverterParameter are passed without any quotes. So remove single quotes from converter parameter.

  2. There is nothing stopping your from sending more than one value into the parameter, as long as you have a delimiter to separate them out later, but you cannot use a comma as that delimits the XAML. So use pipe in such cases and in converter split parameter by pipe |.

Moreover, please note that
a) There has to be a static resource for converter like this in resources.

<local:ValueToVisibilityConverter x:Key="ValueToVisibilityConverter" />

where local is xmlns:local="Your project in which this converter is defined"

Note: A trick eBay used to use in urls, years ago, was to delimit data in the URL with QQ. A double-Q does not naturally occur in text data. If you ever get stuck for a text delimiter that will avoid encoding issues just use QQ... This will not work with split though (which requires single characters, but nice to know) :)

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
  • I then get the error `The unnamed argument "2" must appear before named arguments.` – Hugoagogo Jul 02 '14 at 05:21
  • @Hugoagogo: Read this http://stackoverflow.com/questions/7436156/converterparameter-any-way-to-pass-in-some-delimited-list – Nikhil Agrawal Jul 02 '14 at 05:28
  • @Hugoagogo: Read my changed answer. This should definately solve your problem. – Nikhil Agrawal Jul 02 '14 at 05:41
  • Thanks the link you posted a comment up put me on the right track and I tried exactly what you did, I was just chasing up a secondary bug (`!(value is String)` should be removed from my converter) when you posted your updated answer. Thanks for the ebay tip – Hugoagogo Jul 02 '14 at 05:49
  • As an aside I have gone with pipes but for future reference is there an easy way to escape a comma. – Hugoagogo Jul 02 '14 at 05:51
0

you can pass the ConverterParameter like

  <Binding Path="MyProperty"
             Converter="{StaticResource IntToBoolConverter}">
        <Binding.ConverterParameter>
            <sys:Int32>0</sys:Int32>
        </Binding.ConverterParameter>
    </Binding>
Dhaval Patel
  • 7,471
  • 6
  • 37
  • 70
  • I mentioned in the question I need to be able to check for matches against arbitrary values, I do not want to have to create many virtually identical converters for any combination of values I need to match. – Hugoagogo Jul 02 '14 at 05:24
  • How can this be extended to multiple values – Hugoagogo Jul 02 '14 at 05:36