简体   繁体   中英

User Input Validation Binding

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

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;
        }
    }
}

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}"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM