1

hej,

im not sure how to solve my problem. I want to Validate User Input from my TextBox and then Change the color of my Label if the input is Wrong.Do i need to validate the input in my Get and Set? Or is this totaly Wrong?

my xaml code :

<Label x:Name="lblEmail" Content="Email Adress" Foreground="{Binding EmailAdressValid}"/>
<TextBox x:Name="txtEmail" Text="{Binding EmailAdress, UpdateSourceTrigger=PropertyChanged}"/>

My Data Class:

public class MainData : INotifyPropertyChanged {

        private int _emailAdress;
        public int EmailAdress
        {
            get { return _emailAdress; }
            set
            {
                _emailAdress = value;
                OnPropertyChanged(nameof(EmailAdress));
            }
        }

        private System.Windows.Media.Brush _emailAdressValid;
        public System.Windows.Media.Brush EmailAdressValid
        {
            get { return _emailAdressValid; }
            set
            {
                if(_emailAdress.Contains("@")) {
                   _emailAdressValid = Brushes.Black;
                }
                else {
                    _emailAdressValid = Brushes.Red;
                }
                OnPropertyChanged(nameof(EmailAdressValid));
            }
        }
}
1
  • 1
    Variable _emailAdress is declared as int and later is treated as string (_emailAdress.Contains("@")). What is the intent? Commented Jul 9, 2016 at 12:55

2 Answers 2

1

IMO you are not following a proper way to do validations in WPF. We have IDataErrorInfo for that. See this as a starter.

Now coming to your question, your code says it will never update error brush until you set it, which you do not. In fact you don't need a setter in EmailAdressValid property. Just call OnPropertyChanged for it when email address is provided in view.

public class MainData : INotifyPropertyChanged 
{
    private string _emailAdress;
    public string EmailAdress
    {
        get { return _emailAdress; }
        set
        {
            _emailAdress = value;
            OnPropertyChanged(nameof(EmailAdress));
            OnPropertyChanged(nameof(EmailAdressValid));
        }
    }

    private System.Windows.Media.Brush _emailAdressValid;
    public System.Windows.Media.Brush EmailAdressValid
    {
        get 
        { 
            if(_emailAdress.Contains("@"))
            {
               _emailAdressValid = Brushes.Black;
            }
            else 
            {
                _emailAdressValid = Brushes.Red;
            }

            return _emailAdressValid;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Use Control Binding feature Directly to bind label to the email input text box. Then use the value converter to validate the value oe email and return the required background color. Refer to below code for reference:


public class TextToBackgroundConverter : IValueConverter
{
    public object Convert(object value, Type targetType, 
        object parameter, CultureInfo culture)
    {
        // Validate the email text and retun background color of your choice 
    }
    public object ConvertBack(object value, Type targetType, 
        object parameter, CultureInfo culture)
    {
        // Not needed 
    }
}

Include this resource as static resource in xaml file and use in the binding as below

<Label x:Name="lblEmail" Content="Email Adress" Foreground="{Binding ElementName=txtEmail,Path=Text,Converter={StaticResource bgconverter}}"}"/> <TextBox x:Name="txtEmail" Text="{Binding EmailAdress, UpdateSourceTrigger=PropertyChanged}"/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.