how to pass an integer as ConverterParameter?

93,796

Solution 1

Here ya go!

<RadioButton Content="None"
             xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <RadioButton.IsChecked>
        <Binding Path="MyProperty"
                 Converter="{StaticResource IntToBoolConverter}">
            <Binding.ConverterParameter>
                <sys:Int32>0</sys:Int32>
            </Binding.ConverterParameter>
        </Binding>
    </RadioButton.IsChecked>
</RadioButton>

The trick is to include the namespace for the basic system types and then to write at least the ConverterParameter binding in element form.

Solution 2

For completeness, one more possible solution (perhaps with less typing):

<Window
    xmlns:sys="clr-namespace:System;assembly=mscorlib" ...>
    <Window.Resources>
        <sys:Int32 x:Key="IntZero">0</sys:Int32>
    </Window.Resources>

    <RadioButton Content="None"
                 IsChecked="{Binding MyProperty,
                                     Converter={StaticResource IntToBoolConverter},
                                     ConverterParameter={StaticResource IntZero}}" />

(Of course, Window can be replaced with UserControl, and IntZero may be defined closer to the place of actual usage.)

Solution 3

Not sure why WPF folks tend to be disinclined towards using MarkupExtension. It is the perfect solution for many problems including the issue mentioned here.

public sealed class Int32Extension : MarkupExtension
{
    public Int32Extension(int value) { this.Value = value; }
    public int Value { get; set; }
    public override Object ProvideValue(IServiceProvider sp) { return Value; }
};

If this markup extension is available in XAML namespace 'm', then the original poster's example becomes:

<RadioButton Content="None"
             IsChecked="{Binding MyProperty,
                         Converter={StaticResource IntToBoolConverter},
                         ConverterParameter={m:Int32 0}}" />

This works because the markup extension parser can see the strong type of the constructor argument and convert accordingly, whereas Binding's ConverterParameter argument is (less-informatively) Object-typed.

Solution 4

Don't use value.Equals. Use:

  Convert.ToInt32(value) == Convert.ToInt32(parameter)
Share:
93,796

Related videos on Youtube

akonsu
Author by

akonsu

Updated on November 26, 2020

Comments

  • akonsu
    akonsu over 3 years

    I am trying to bind to an integer property:

    <RadioButton Content="None"
                 IsChecked="{Binding MyProperty,
                             Converter={StaticResource IntToBoolConverter},
                             ConverterParameter=0}" />
    

    and my converter is:

    [ValueConversion(typeof(int), typeof(bool))]
    public class IntToBoolConverter : IValueConverter
    {
        public object Convert(object value, Type t, object parameter, CultureInfo culture)
        {
            return value.Equals(parameter);
        }
    
        public object ConvertBack(object value, Type t, object parameter, CultureInfo culture)
        {
            return value.Equals(false) ? DependencyProperty.UnsetValue : parameter;
        }
    }
    

    the problem is that when my converter is called the parameter is string. i need it to be an integer. of course i can parse the string, but do i have to?

    thanks for any help konstantin

    • Krzysztof Kaczor
      Krzysztof Kaczor over 10 years
      Does anybody know how to achive this on Windows Phone platform where we have slightly different syntax for bindings? {Binding PROPERTY, Converter={StaticResource MYCONVERTER}, ConverterParameter=INT_VAL} in this example INT_VAL will be passed as a string
  • Dan J
    Dan J almost 13 years
    This doesn't change the fact that the type of IValueConverter.Convert()'s "parameter" parameter is object. You still have to cast/parse it...
  • jpierson
    jpierson almost 13 years
    @djacobson - True but that is what the ValueConversion attribute allows you to specify. Not exactly sure whether this is really used by at compile-time or run-time at all. In terms of the original posters question he specified that "i need it to be an integer. of course i can parse the string, but do i have to?" So my answer alleviates that in that there is no parsing of a string but only the unboxing of an integer which I is still much more safe.
  • Zack
    Zack almost 10 years
    Why don't you want to use value.Equals?
  • Zeus
    Zeus over 8 years
    Thanks, that was useful. It will be my first XAML extension. But I think it's better to make Value an object rather than int, to avoid boxing it each time in ProvideValue. (And then, make it private to avoid assigning something illegal directly).
  • Glenn Slayden
    Glenn Slayden about 8 years
    @Zeus Typically ProvideValue is only called once per markup extension instance, so the boxing should only occur once anyway. By not doing it in the constructor, I avoid boxing altogether if ProvideValue is never called. As for making Value private, this would preclude using the markup extension in XAML object element syntax: msdn.microsoft.com/en-us/library/…
  • H.B.
    H.B. almost 8 years
    Your last paragraph is wrong. It's called type conversion and it also applies to properties. The thing is, that Binding.ConverterParameter has no specific type (it's just an object) so the parser does not know what conversion to apply, hence every literal is just treated as a string.
  • H.B.
    H.B. almost 8 years
    (So if you extension had a default constructor, then {m:Int32 Value=0} would work just as well.)