如何将整数作为 ConverterParameter 传递? [英] how to pass an integer as ConverterParameter?

查看:24
本文介绍了如何将整数作为 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?

感谢您的帮助康斯坦丁

推荐答案

给你!

<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天全站免登陆