Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4560192
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:53:45+00:00 2026-05-21T17:53:45+00:00

I have a WPF textbox on a form to allow input of a URI.

  • 0

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, 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);
            }
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T17:53:46+00:00Added an answer on May 21, 2026 at 5:53 pm

    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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form in WPF with 2 textbox : <TextBox Name=txtName Text={Binding Contact.Name}/>
I have a WPF application with form that has a textbox named txtStatusWindow. I
I have a WPF MVVM app that contains a data entry form with several
I have a WPF textBox that is declared as ReadOnly <TextBox IsReadOnly=True IsTabStop=False Width=200
I have a WPF TextBox in which I want to allow autocompleting user names
This other SO question asks about an autocomplete textbox in WPF. Several people have
I have a WPF form with a combobox and a textbox (both are databound
I have a simple control that extends the WPF TextBox control. The basic idea
I have a WPF Window that contains a TextBox. I have implemented a Command
I have a WPF / XAML form data-bound to a property in a dictionary,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.