0

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;
    }
 }
H.B.
  • 166,899
  • 29
  • 327
  • 400
bswee
  • 81
  • 1
  • 1
  • 8

3 Answers3

3

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.

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • Interesting so this looks like something that you will avoid. – bswee May 03 '11 at 19:08
  • Not necessarily, it all depends on whether you need the reverse conversion. – H.B. May 03 '11 at 19:09
  • So do you agree with infensus and avoid using valueconverter to do this? – bswee May 03 '11 at 19:11
  • Yeah I will have to do the reverse conversion. – bswee May 03 '11 at 19:11
  • What exactly are you trying to do? – H.B. May 03 '11 at 19:14
  • Display the object in the textbox with two-way binding. That's why I have to convert it from object to string and reverse. – bswee May 03 '11 at 19:21
  • I really doubt that, you should be able to bind directly to the internal property that is relevant rather than the whole object. – H.B. May 03 '11 at 19:22
  • In xaml, Binding Path="JobKey" and in a public class:public BasePar JobKey { get { return jobKey; } set { jobKey = value; } } – bswee May 03 '11 at 19:26
  • That's I'm doing. But Output Debug says it needs a converter for the value to display correctly because the source is object and target is string – bswee May 03 '11 at 19:29
  • JobKey is just an `object`, nothing specific? – H.B. May 03 '11 at 19:31
  • Ok talked to the other guy. He is going to fixed his basepar because his basepar is type specific and convert back doesnt give type info.Thanks for all the help. Going to stick with just standard type for now. – bswee May 03 '11 at 20:00
  • Well, it should be quite obvious that you cannot **sufficiently** manipulate data of type `object` with a TextBox, if anything can be used as a key then messing with that in a TextBox is not a viable option. If all keys are strings the property should be of type string as well. – H.B. May 03 '11 at 20:06
1

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;
trydis
  • 3,905
  • 1
  • 26
  • 31
1

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().

Dominic
  • 62,658
  • 20
  • 139
  • 163
  • It's not me, it is a model created by another. He created this BasePar class to read different types. – bswee May 03 '11 at 19:09