Using WPF TextBox with a URI converter, invalid input wipes textbox
I have a WPF textbox on a form to allow input of a URI.
I tried to do this using a data converter. The problem is that when the textbox binding updates and the textbox doesn't contain a valid URI,
- The data converter returns a null ;
- which sets my model property to null ;
- which results in a property changed event firing ;
- which sets the text box value to the empty string, wiping out the users invalid input
I'm a WPF novice, and I'm at a loss to find a simple pattern using a data converter that doesn't result in this behaviour. I'm thinking there must be a standard pattern to use, that I'd know about if I was an experienced WPF programmer.
Looking at the examples included with Prism 4, there seems to be two different approaches used. I dislike them both.
The first approach is to throw an exception when my model property is set null, which is caught and shown as a validation error. The problem is that I want the property to be able to be set to null - each time you open the form, the fields are set to their previous values. If the application has never been ran before, the URI will be set to null - this shouldn't throw an exception. Also, the use of exceptions for validation is ugly.
The second approach is when the property is set to null, set the validation state of the model to include the property invalidity, but don't actually update the property. Which I think is awful. It results in the model being internally inconsistent, claiming that the DCSUri is invalid, but containing the previous valid value of DCSUri.
The approach I'm using to avoid these issues is to have a string DCSUri in my ViewModel, which only updates the Uri typed DCSUri property of my Model if it is a valid URI. But I'd prefer an approach which allows use of a converter and binding my textbox directly to my model.
My converter code:
/// <summary>
/// Converter from Uri to a string and vice versa.
/// </summary>
public class StringToUriConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
Uri uri = null;
string stringValue = value as string;
if (stringValue != null)
Uri.TryCreate(stringValue, UriKind.Absolute, out uri);
return uri;
}
}
The XAML for the textbox:
<TextBox Grid.Row="1" Grid.Column="1" Name="DCSUriTextBox"
Text="{Binding Path=DCSLoadSettings.DCSUri, Mode=TwoWay, UpdateSourceTrigger=LostFocus, 开发者_JAVA技巧ValidatesOnExceptions=True, NotifyOnValidationError=True, ValidatesOnDataErrors=True, Converter={StaticResource StringToUriConverter} }"
HorizontalAlignment="Stretch" Height="Auto" VerticalAlignment="Center" Margin="5,0,20,0" IsReadOnly="{Binding Path=IsNotReady}" Grid.ColumnSpan="2" />
And the code for the DCSUri property within my model:
/// <summary>
/// The Uri of the DCS instance being provided configuration
/// </summary>
public Uri DCSUri
{
get
{
return mDCSUri;
}
set
{
if (!Equals(value, mDCSUri))
{
mDCSUri = value;
this["DCSUri"] = value == null
? "Must provide a Uri for the DCS instance being provided configuration"
: string.Empty;
RaisePropertyChanged(() => DCSUri);
}
}
}
You should use ValidationRules for Validation, and name your converters the right way around; i would approach it like this (assuming that you want to be able to set the Uri to null
):
public class UriToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Uri input = value as Uri;
return input == null ?
String.Empty : input.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string input = value as string;
return String.IsNullOrEmpty(input) ?
null : new Uri(input, UriKind.Absolute);
}
}
public class UriValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
string input = value as string;
if (String.IsNullOrEmpty(input)) // Valid input, converts to null.
{
return new ValidationResult(true, null);
}
Uri outUri;
if (Uri.TryCreate(input, UriKind.Absolute, out outUri))
{
return new ValidationResult(true, null);
}
else
{
return new ValidationResult(false, "String is not a valid URI");
}
}
}
Then use it like this (or by defining the converter and rule as a resource somewhere):
<TextBox MinWidth="100">
<TextBox.Text>
<Binding Path="Uri">
<Binding.ValidationRules>
<vr:UriValidationRule />
</Binding.ValidationRules>
<Binding.Converter>
<vc:UriToStringConverter/>
</Binding.Converter>
</Binding>
</TextBox.Text>
</TextBox>
If the input text does not pass validation the Converter will not be called, that is why i have no TryCreate
or anything like that in there.
There is a decent article about input validation on CodeProject which you might find to be helpful.
To test the value for null you could use another converter and a helper TextBlock:
public class NullToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value == null ?
"NULL" : value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
<TextBlock>
<TextBlock.Text>
<Binding Path="Uri">
<Binding.Converter>
<vc:NullToStringConverter/>
</Binding.Converter>
</Binding>
</TextBlock.Text>
</TextBlock>
精彩评论