c# - 如何在转换器中将对象转换为 int?

标签 c# wpf converter

我放弃了,我怎么投这个?

class AmountIsTooHighConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        //int amount = (int)value;
        //int amount = (int)(string)value;
        //int amount = (int)(value.ToString);
        //int amount = Int32.Parse(value);
        //int amount = (int)Convert.ChangeType(value, typeof(int));
        //int amount = Convert.ToInt32(value);
        if (amount >= 35)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }
}

最佳答案

Convert.ToInt32Int32.Parse 都应该工作...如果他们不工作,那么该值肯定不是 int ;)
尝试在你的转换器中放置一个断点来观察值,它可能会告诉你为什么它不起作用

关于c# - 如何在转换器中将对象转换为 int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/915457/

相关文章:

c# - 登录操作后如何控制页面的可缓存性

c# - 隐藏打印页面中的特定列c#

wpf - 无法通过引用找到绑定(bind)源

c# - 如何阻止 RichTextBox Inline 在 TextChanged 中被删除?

c# - WPF转换器在文本更改时实时更新文本框的背景颜色

c# - EF : The underlying provider failed on Open

c# - WPF RTP 流

spring - 推土机自定义转换器ID映射: Object to Long and Long to Object via DozerConverter getParameter

java - 更简单的 CSV 到 SQL java 文件转换器

c# - 为什么 '\0' .ToString()==string.Empty 返回 FALSE?