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?
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());
}
}