Johannes Wanzek
Johannes Wanzek

Reputation: 2875

Textbox binding to object breaks binding (type)

In my Application I have a problem which I simulated with a little demo application.

I have a ViewModel for a ListBoxItem which has a Value Property of Type object. This can be Int32, Decimal, String ...

When I load the Value and get it without edit it stays the same Type

enter image description here

When I append a 4 into the textbox and get the value

enter image description here

The Property becomes a String.

Why is my TextBox binding changing the type of my bound object when I edit the value?

edit: This is the Property:

public int TestProperty
        {
            get { return _testProperty; }
            set
            {
                if (Equals(value, _testProperty)) return;
                _testProperty = value;
                OnPropertyChanged();
            }
        }

And I assign Int32 Value 123 to it:

TestProperty = 123;

Before and after typing into the textbox I call this line:

 StatusMessage = string.Format("Current Type: {0} Value: {1}", _testProperty.GetType().Name, _testProperty);

edit 2: It works with this (Setting x:Shared to false):

 public class PreserveTypeValueConverter : IValueConverter
    {
        public Type Type { get; private set; }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                Type = value.GetType();
                return System.Convert.ChangeType(value, targetType, culture);
            }
            catch (Exception)
            {
                return new ValidationResult(false, string.Format("Cannot convert {0} to {1}", value.GetType().Name, targetType.Name));
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                return System.Convert.ChangeType(value, Type, culture);
            }
            catch (Exception)
            {
                return new ValidationResult(false, string.Format("Cannot convert {0} to {1}", value.GetType().Name, targetType.Name));
            }
        }
    }

Upvotes: 0

Views: 128

Answers (2)

user666
user666

Reputation: 226

You input text data. If your datasource implemented like int, TextBox has default settings to apply ValidationError and use default converter for default types. But for unknown types, which have not CLR metadata, it simply calls .ToString() when you Convert/ConvertBack value from TextBox to your property or visa versa. If you want, you can write your own converter IValueConverter.

Upvotes: 1

Sheridan
Sheridan

Reputation: 69959

The .NET Framework will convert your int to a string to display in the TextBox. I can only imagine that your actual property type is object, so the Framework wouldn't know what type (eg. int) to convert it back to from the string. It would work if you used a strongly typed property, eg. of type int.

Upvotes: 1

Related Questions