IValue 转换器:ConvertBack - 字符串到对象
我可以将对象从对象转换为文本框。但我在转换回来时有点失落。非常感谢任何领导或帮助。
[ValueConversion(typeof(Object), typeof(String))]
public class DataConverter : IValueConverter
{
// This converts the DateTime object to the string to display.
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
BasePar data = (BasePar)value;
return data.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
string strValue = value as string;
Object objString = (Object)strValue;
return objString;
}
}
I can get convert to work from an object to a textbox. But I am a little loss at convertback. Any lead or help is much appreciated.
[ValueConversion(typeof(Object), typeof(String))]
public class DataConverter : IValueConverter
{
// This converts the DateTime object to the string to display.
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
BasePar data = (BasePar)value;
return data.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
string strValue = value as string;
Object objString = (Object)strValue;
return objString;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一般来说:你不能假设每次转换都是无损的,尤其是
ToString
通常恰恰相反,除非你的对象非常简单,否则你将无法重建它。简单的例子,将数字转换为其数字和:
2584 -> 19
,向后转换是不确定的,因为唯一映射只是单向的。数字和为19
的数字有很多,但2584
只有一位数字和。Just generally speaking: You cannot assume that every conversion is lossless, especially
ToString
is normally quite the opposite, unless your object is very simple you will not be able to reconstruct it.Simple example, convert number to its digit-sum:
2584 -> 19
, the backwards conversion is indeterminate because the unique mapping is one-way only. There are quite a lot of numbers with a digit sum of19
but2584
has only one digit sum.尝试一些类似的事情:
Try something along the lines:
确实如 HB 所说,您为什么要尝试在文本框对象和字符串之间进行转换?看来您可能需要查看您的设计 - 它被称为值转换器是有原因的!如果您确实想这样做,请研究类序列化 - 序列化为 MemoryStream 并反序列化为对象。您也可以从字符串(http://stackoverflow.com/questions/2347642/deserialize-from-string-instead-textreader)反序列化,但是既然您不想显示这种字符串,为什么还要麻烦呢?如果出于某种疯狂的原因,您确实想要序列化为字符串,您可以将内存位置设置为 0 并将内存流传递给 StreamReader,然后调用 StreamReader.ReadToEnd().ToString()。
indeed what H.B. said, why are you trying to convert between a textbox object and a string anyway? Seems like you might need to look at your design - its called value converter for a reason! If you really want to do it look into class serialization - serialize to a MemoryStream and deserialize to an object. You can deserialize from a string (http://stackoverflow.com/questions/2347642/deserialize-from-string-instead-textreader) too but why bother since you wouldn't want to display that kind of a string anyway? If for some mad reason you do want to serialize into a string you can set the memory position to 0 and pass the memory stream to a StreamReader and then call StreamReader.ReadToEnd().ToString().