【发布时间】:2011-11-29 21:56:15
【问题描述】:
我刚刚做了我的第一个转换器来从 int 转换为 string。我有一个用整数(年)填充的组合框,但如果值为 0,我希望组合框显示“全部”。
这是我的转换器:
public class IntToString : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null)
{
int intY = (int)value;
if (intY == 0)
{
String strY = "All";
return strY;
}
else
{
return intY.ToString();
}
}
return String.Empty;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
}
}
在 XAML 中我应该在哪里设置转换器?我在组合框的 ItemsSource 中试过:
ItemsSource="{Binding YearsCollection, Converter={StaticResource intToStringYearConverter}}"
但我总是在这一行得到InvalidcastException:
int intY = (int)value;
【问题讨论】: