问题描述
你可能知道,Silverlight 3不支持IMultiValueConverter和...我非常需要它。 Web Service代理,定义了我需要在我的UI中显示的类结构。对象定义类有一些数组属性,如string [],int []等。当我将这些属性绑定到一个TextBlock时,TextBlock的Text属性变为System.String []或System.Int []。相反,我想看到一个列表字符串或数字用逗号分隔。我曾考虑过使用一个IMultiValueConverter,但Silverlight 3不支持它。如何解决这个问题?
谢谢
的 IMultiValueConverter
是实现支持多重绑定的转换器(即 MultiBinding
对象)。在你的情况下,这实际上并不是你需要的。
如果要转换数组( string []
)转换为文本值,然后简单地定义一个正常的 IValueConverter
。不要让数组包含多个值的事实让你感到困惑。
这是一个示例转换器:
[ValueConversion(typeof(string []),typeof(string))]
public class StringArrayConverter:VIalueConverter
{
public object Convert(object value,Type targetType,object parameter,CultureInfo culture)
{
return string.Join(,,(string [])value);
}
public object ConvertBack(object value,Type targetType,object parameter,CultureInfo culture)
{
throw new NotImplementedException();
}
}
希望有所帮助。
As you may know, Silverlight 3 doesn't support IMultiValueConverter and... I badly need it. A Web Service proxy which defines a class structure that I need to display in my UI. The object definition class has a few array property such as string[], int[], etc. When I bind these property to a TextBlock, the Text property of the TextBlock becomes System.String[] or System.Int[]. Instead, I would like to see a list strings or numbers separated by a comma.
I thought about using a IMultiValueConverter but Silverlight 3 doesn't support it. How do I work around this?
Thanks
The purpose of IMultiValueConverter
is to implement converters that support multiple bindings (i.e. MultiBinding
objects). In your case, this doesn't actually seem to be what you need.
If you want to convert an array (string[]
for example) into a text value, then simply define a normal IValueConverter
that does that. Don't let the fact that an array contains multiple values confuse you.
Here's an example converter:
[ValueConversion(typeof(string[]), typeof(string))]
public class StringArrayConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return string.Join(", ", (string[])value);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Hope that helps.
这篇关于Silverlight 3和IMultiValueConverter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!