4

I want to be able to override the default textbox validation for convertion and handle it myself. I've looked at validation rules but I can't get it to disable the original validation. Xaml so far:

<Grid>
    <TextBox Text="{Binding Path=Option.Value, NotifyOnSourceUpdated=True}" >
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SourceUpdated">
                <i:InvokeCommandAction Command="{Binding OptionValueChanged}"></i:InvokeCommandAction>
       </i:EventTrigger>
        </i:Interaction.Triggers>
    </TextBox>
</Grid>

Atm, when a string is entered it displays 'Value {val} cannot be converted' as the field is an integer. How can this be disabled to handle the value myself?

matt
  • 67
  • 1
  • 10
  • 1
    If you want to handle the conversion between a string and an int yourself you could use a converter. – mm8 Mar 23 '17 at 11:32

2 Answers2

5

An int property can only be set to an int value and nothing else. You could customize the error message but you won't ever be able to set the int property to something else than an int.

Please refer to the answer here for an example of how to use a custom ValidationRule to customize the error message:

How do I handle DependencyProperty overflow situations?

If you want to handle the actual conversion between the string and the int yourself you could use a converter:

<Window.Resources>
    <local:YourConverter x:Key="conv" />
</Window.Resources>
...
<TextBox Text="{Binding Path=Option.Value, NotifyOnSourceUpdated=True, Coverter={StaticResource conv}}" / >

public class YourConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        //convert the int to a string:
        return value.ToString();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        //convert the string back to an int here
        return int.Parse(value.ToString());
    }
}
Community
  • 1
  • 1
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Thanks, I wouldn't want to convert it to a string but if the user was to specify a value that was not an int it would default to say 0 and I would be able to handle the message myself – matt Mar 23 '17 at 11:43
  • Use a custom validation rule as per the link then. – mm8 Mar 23 '17 at 11:44
  • Will do, I'm just trying it out now – matt Mar 23 '17 at 14:55
0

I had this same issue, and adding TargetNullValue='' didn't work. Adding a converter like the accepted answer here suggests didn't work ither. However, in my case, the issue was with a ContactId and when the Customers ComboBox was changed it populated the Contacts. When the Contacts ObserveableCollection<Contact>.Clear() was called it was throwing this error.

My fix was to ensure that the ContactId had changed before ObserveableCollection<Contact>.Clear() and ObserveableCollection<Contact>.Add(contact) was called.

Dharman
  • 30,962
  • 25
  • 85
  • 135
3xGuy
  • 2,235
  • 2
  • 29
  • 51