如何通过一个整数作为ConverterParameter? [英] how to pass an integer as ConverterParameter?

查看:572
本文介绍了如何通过一个整数作为ConverterParameter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图绑定到一个整数属性:

I am trying to bind to an integer property:

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

和我的转换器是:

[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

推荐答案

下面雅去!

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

关键是要包括基本系统类型名称空间,然后写入至少ConverterParameter的元素结合的形式。

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

这篇关于如何通过一个整数作为ConverterParameter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆