wpf - 如何传递整数作为 ConverterParameter?

标签 wpf binding ivalueconverter

我正在尝试绑定(bind)到整数属性:

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

问题是当我的转换器被调用时参数是字符串。我需要它是一个整数。我当然可以解析字符串,但是我必须这样做吗?

感谢您的帮助 康斯坦丁

最佳答案

给你!

<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 绑定(bind)。

关于wpf - 如何传递整数作为 ConverterParameter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3978937/

相关文章:

c# - 字节到 bool 值的转换器(复选框)

wpf - WPF TabControl内存问题

服务中的 WPF FormattedText "The system cannot find the file specified"异常

c# - 如何从 C# 代码中调用 Command 而不是通过将 UI 控件绑定(bind)到它?

绑定(bind)路径中的 WPF 强制转换

java - 如何从元素绑定(bind)到列表的 JTable 检索 "${selectedElement}"

c# - MVVM跨WPF区域实现

字符串文字中的 SQLite 绑定(bind)

c# - 如何在单独的线程上运行 Converter 内的代码,以便 UI 不卡住?

wpf IValueConverter 不更新 View