how to pass an integer as ConverterParameter? how to pass an integer as ConverterParameter? wpf wpf

how to pass an integer as ConverterParameter?


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.


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.)


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.