IsChecked 需要一个布尔值(真/假),但表包含数字类型。您需要将 ValueConverter 添加到将数值转换为布尔值的绑定语句。
检查How to bind a boolean to a combobox in WPF for the inverse case (convert an bool to an int). 在您的情况下, ValueConverter 应该是:
public class NumToBoolConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((int)value == 1);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool)value ? 1 : 0;
}
}
}
更新
这篇文章有一个 NumToBoolConverter,它也进行类型和空值检查:
public class NumToBoolConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
if (value!=null && value is int )
{
var val = (int)value;
return (val==0) ? false : true;
}
return null;
}
public object ConvertBack(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
if (value!=null && value is bool )
{
var val = (bool)value;
return val ? 1 : 0;
}
return null;
}
#endregion
}