How to pass an integer as ConverterParameter?

I am trying to bind an integer property:

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

and my converter:

 [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 a parameter, it is a string. I need it to be an integer. Of course, I can parse the string, but do I need?

thanks for any help

+74
wpf binding ivalueconverter
Oct 20 '10 at 14:30
source share
5 answers

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 a namespace for the main types of the system, and then write at least the ConverterParameter binding as an element.

+82
Aug 02 2018-11-11T00:
source share

For completeness, another possible solution (possibly with a smaller character set):

 <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 can be defined closer to the place of actual use.)

+35
Nov 10 '14 at
source share

Not sure why WPF people tend not to use MarkupExtension . This is an ideal solution for many problems, including the problem 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 the XAML namespace 'm', then the original poster example would look like this:

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

This works because the extension parser parser can see the strong type of the constructor argument and convert it accordingly, while the Binding ConverterParameter argument is (less informative) set on the subject.

+19
Apr 01
source share

Do not use value.Equals . Using:

  Convert.ToInt32(value) == Convert.ToInt32(parameter) 
+4
Oct 20 2018-10-10
source share

It would be nice to somehow express the type information for ConverterValue to XAML, but I don't think this is possible at the moment. Therefore, I think you need to analyze the converter object for your expected type using some custom logic. I do not see another way.

0
Oct 20 2018-10-10
source share



All Articles