IValue 转换器:ConvertBack - 字符串到对象

发布于 2024-11-04 16:37:51 字数 644 浏览 6 评论 0原文

我可以将对象从对象转换为文本框。但我在转换回来时有点失落。非常感谢任何领导或帮助。

 [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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼泪也成诗 2024-11-11 16:37:51

一般来说:你不能假设每次转换都是无损的,尤其是 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 of 19 but 2584 has only one digit sum.

浮华 2024-11-11 16:37:51

尝试一些类似的事情:

var textBoxValue = value as string;
if(textBoxValue != null) {
    // Create BasePar instance, setting the textBoxValue as a property value or whatever and return it
}
return DependencyProperty.UnsetValue;

Try something along the lines:

var textBoxValue = value as string;
if(textBoxValue != null) {
    // Create BasePar instance, setting the textBoxValue as a property value or whatever and return it
}
return DependencyProperty.UnsetValue;
风为裳 2024-11-11 16:37:51

确实如 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().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文