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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3978937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:15:10  来源:igfitidea点击:

how to pass an integer as ConverterParameter?

wpfbindingivalueconverter

提问by akonsu

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

感谢康斯坦丁的任何帮助

回答by jpierson

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.

诀窍是包含基本系统类型的命名空间,然后至少以元素形式编写 ConverterParameter 绑定。

回答by Vlad

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, Windowcan be replaced with UserControl, and IntZeromay be defined closer to the place of actual usage.)

(当然,Window可以换成UserControlIntZero也可以根据实际使用的地方来定义。)

回答by Glenn Slayden

Not sure why WPFfolks tend to be disinclined towards using MarkupExtension. It is the perfect solution for many problems including the issue mentioned here.

不知道为什么WPF人们往往不愿意使用MarkupExtension. 它是许多问题的完美解决方案,包括此处提到的问题。

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 XAMLnamespace 'm', then the original poster's example becomes:

如果此标记扩展在XAML命名空间“m”中可用,则原始海报的示例变为:

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

这是有效的,因为标记扩展解析器可以看到构造函数参数的强类型并相应地进行转换,而 Binding 的 ConverterParameter 参数是(信息较少)对象类型的。

回答by Aliostad

Don't use value.Equals. Use:

不要使用value.Equals. 用:

  Convert.ToInt32(value) == Convert.ToInt32(parameter)

回答by SKG

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.

以某种方式在 XAML 中表达 ConverterValue 的类型信息会很好,但我认为目前不可能。所以我猜你必须通过一些自定义逻辑将 Converter Object 解析为你期望的类型。我没有看到其他方式。