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
For completeness, one more possible solution (perhaps with less typing):
(Of course,
Window
can be replaced withUserControl
, andIntZero
may be defined closer to the place of actual usage.)Not sure why
WPF
folks tend to be disinclined towards usingMarkupExtension
. It is the perfect solution for many problems including the issue mentioned here.If this markup extension is available in
XAML
namespace 'm', then the original poster's example becomes: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.
Don't use
value.Equals
. Use:It would be nice to somehow express the type information for the ConverterValue in XAML, but I don't think it is possible as of now. So I guess you have to parse the Converter Object to your expected type by some custom logic. I don't see another way.
Here ya go!
The trick is to include the namespace for the basic system types and then to write at least the ConverterParameter binding in element form.