如何传递整数作为 ConverterParameter?
我试图绑定到一个整数属性:
<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;
}
}
问题是当我的转换器被调用时参数是字符串。我需要它是一个整数。我当然可以解析字符串,但是我必须这样做吗?
感谢您的帮助 康斯坦丁
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
给你!
诀窍是包含基本系统类型的命名空间,然后至少以元素形式编写 ConverterParameter 绑定。
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.
为了完整起见,还有一种可能的解决方案(也许需要更少的打字):(
当然,
Window
可以替换为UserControl
,并且IntZero
可以是定义更接近实际使用的地方。)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.)不知道为什么
WPF
人们往往不愿意使用MarkupExtension
。它是许多问题的完美解决方案,包括这里提到的问题。如果此标记扩展在
XAML
命名空间“local:
”中定义,则原始发布者的示例将变为:这有效,因为标记扩展解析器可以看到构造函数的强类型参数并进行相应的转换,而 Binding 的 ConverterParameter 参数是(信息较少)对象类型的。
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 defined in
XAML
namespace 'local:
', 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.
不要使用
value.Equals
。使用:Don't use
value.Equals
. Use:如果能以某种方式在 XAML 中表达 ConverterValue 的类型信息就好了,但我认为目前还不可能。所以我想你必须通过一些自定义逻辑将转换器对象解析为你期望的类型。我看不到其他方法。
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.