how to pass an integer as ConverterParameter?
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)
Related videos on Youtube
akonsu
Updated on November 26, 2020Comments
-
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 over 10 yearsDoes 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 almost 13 yearsThis doesn't change the fact that the type of
IValueConverter.Convert()
's "parameter" parameter isobject
. You still have to cast/parse it... -
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 almost 10 yearsWhy don't you want to use
value.Equals
? -
Zeus over 8 yearsThanks, that was useful. It will be my first XAML extension. But I think it's better to make
Value
anobject
rather thanint
, to avoid boxing it each time inProvideValue
. (And then, make itprivate
to avoid assigning something illegal directly). -
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 ifProvideValue
is never called. As for makingValue
private, this would preclude using the markup extension inXAML
object element syntax: msdn.microsoft.com/en-us/library/… -
H.B. almost 8 yearsYour 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 anobject
) so the parser does not know what conversion to apply, hence every literal is just treated as a string. -
H.B. almost 8 years(So if you extension had a default constructor, then
{m:Int32 Value=0}
would work just as well.)