如何在转换器中将对象转换为 int?
我放弃了,我该怎么投呢?
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;
}
}
I give up, how do I cast this?
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Convert.ToInt32
或Int32.Parse
都应该可以工作...如果不能,那么该值绝对不是 int ;)尝试在转换器中放置一个断点来观察该值,它可能会告诉您为什么它不起作用
Both
Convert.ToInt32
orInt32.Parse
should work... If they don't, then the value is definitely not an int ;)Try to put a breakpoint in your converter to watch the value, it might show you why it doesn't work
如果该值实际上是一个字符串对象(具有表示整数值的内容),则这会产生最小的开销:
Convert.ToInt32
应该能够处理大多数可能转换为int
,就像包含数字或int
可以处理的范围内的任何数字类型的字符串。If the value is actually a string object (with a content that represents an integer value), this gives the least overhead:
The
Convert.ToInt32
should be able to handle most anything that is possible to convert to anint
, like a string containing digits or any numeric type that is within the range that anint
can handle.如果值是一个对象,
ToString()
最好先转换为字符串,然后再解析为Int
int amount = Int32.Parse(value.ToString());
If the value is an object,
ToString()
is better to convert to string before parsing toInt
int amount = Int32.Parse(value.ToString());
在 Convert 方法的左括号处放置一个断点。 在调试模式下启动您的应用程序。 当断点被命中时,分析值的实际类型。 做演员。 如果 value 是字符串,则将其转换为字符串,然后解析结果以获取整数。 再次运行程序。 这次应该可以了。
Put a breakpoint at the opening parenthesis of your Convert method. Start your application in debug mode. When the breakpoint is hit analyze the actual type of value. Do the cast. If value is string cast it to string and then parse the result to get an integer. Run the program again. It should work this time.
答案基本上很简单。 因为
IValueConverter
本身有一个名为Convert
的方法,所以您不能像这样简单地使用Convert
对象。但如果您添加正确的前缀,它就会起作用:
The answer is basically simple. Because the
IValueConverter
has itself a method calledConvert
you cannot simply us theConvert
object just like that.But if you add the correct prefix it works:
我认为这是最安全的方法!
This is safest way, I think!