我剛把我的第一個轉換器從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