我刚把我的第一个转换器从int转换为字符串。我有一个组合框填充整数(年),但如果值为0,我希望组合框显示'全部'。哪里可以设置转换为XAML集合中的项目
这是我的转换器:
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;
我改变了设置它为selectedValue,则是好? – Kobe