我刚刚编写了我的第一个将整数转换为字符串的转换器。我有一个下拉框,里面填充了整数(年份),但如果值为0,我希望下拉框显示“全部”。
这是我的转换器:
在XAML中,我应该在哪里设置转换器?我尝试在组合框的ItemsSource中设置:
但是我总是在这一行收到“InvalidCastException”异常:
这是我的转换器:
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;