I decided to validate the Login field using IDataErrorInfo, implemented the interface and condition, but there is still no check for a blank field in the window? What could be the problem?

Viewmodel

public interface IDataErrorInfo { string Error { get; } string this[string columnName] { get; } } public class Autorized : ViewModelBase, IDataErrorInfo { public ICommand LogIn { get; set; } public Action CloseAction { get; set; } public User user { get; set; } public static string LoginStmp { get; set; } public static string PasswordStmp { get; set; } public static string ServerSmtp { get; set; } public ObservableCollection<string> servers_pop { get; set; } public ObservableCollection<string> servers_smtp { get; set; } private string selectedServerPOP; public string SelectedServerPOP { get { return selectedServerPOP; } set { selectedServerPOP = value; OnPropertyChanged("selectedServerPOP"); } } private string selectedServerSMTP; public string SelectedServerSMTP { get { return selectedServerSMTP; } set { selectedServerSMTP = value; OnPropertyChanged("selectedServerSMTP"); } } public Autorized() { user = new User(); servers_pop = new ObservableCollection<string>(); servers_smtp = new ObservableCollection<string>(); LogIn = new RelayCommand(arg => ToLogIn()); ServersPop(); ServersSmtp(); } public void ToLogIn() { LogPass.GetInstance.Authentication(user.Login, user.Password, SelectedServerPOP); View.View v = new View.View(); SendViewModelAdapter svma = new SendViewModelAdapter(); v.DataContext = svma; LoginStmp = user.Login; PasswordStmp = user.Password; ServerSmtp = SelectedServerSMTP; v.Show(); CloseAction(); } public void ServersPop() { servers_pop.Add("pop.yandex.ru"); servers_pop.Add("pop.mail.ru"); servers_pop.Add("pop.gmail.com"); } public void ServersSmtp() { servers_smtp.Add("smtp.yandex.ru"); servers_smtp.Add("smtp.mail.ru"); servers_smtp.Add("smtp.gmail.com"); } public string Error { get { throw new NotImplementedException(); } } public string this[string columnName] { get { if (columnName == "Login" && string.IsNullOrEmpty(user.Login)) return "введите наименование"; return null; } } } 

View

 <Controls:MetroWindow x:Class="MailClient.View.Menu" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:MailClient.View" mc:Ignorable="d" Title="Menu" Height="350" Width="525"> <Grid Background="#FFAED5F5"> <Label Content=" Почтовый адрес" Grid.Row="0" Grid.Column="0" Margin="73,103,293,189" /> <Button x:Name="button" Content="Войти!" Command="{Binding LogIn}" Margin="217,233,217,0" VerticalAlignment="Top" Height="30"/> <TextBox x:Name="Login" Text="{ Binding Path = user.Login, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" ToolTip="Адрес электронной почты" Height="35" Margin="184,103,183,0" TextWrapping="Wrap" VerticalAlignment="Top"/> <TextBox x:Name="textBox_Copy" Text="{ Binding Path = user.Password, Mode=TwoWay}" Height="35" Margin="184,143,183,0" TextWrapping="Wrap" VerticalAlignment="Top" ToolTip="Пароль"/> <ComboBox x:Name="comboBox" ItemsSource="{Binding servers_pop}" SelectedItem="{Binding SelectedServerPOP}" Margin="10,54,387,0" VerticalAlignment="Top" ToolTip="Pop server"/> <ComboBox x:Name="comboBox_Copy" ItemsSource="{Binding servers_smtp}" SelectedItem="{Binding SelectedServerSMTP}" Margin="10,23,387,0" VerticalAlignment="Top" ToolTip="Smtp server"/> <Label Content=" Пароль" Grid.Row="0" Grid.Column="0" Margin="73,143,293,141" /> </Grid> </Controls:MetroWindow> 

    1 answer 1

    There are quite a few errors in this code.

    1. The IDataErrorInfo interface does not need to be created. It lies in the System.ComponentModel assembly.

    2. This interface is implemented by your ViewModel, which means that validation is on its fields, although there is no such field as Login in your view model.

      Generally a very good example of how to use IDataErrorInfo is on MSDN

    3. Not relevant to the issue. OnPropertyChanged it is case sensitive. And in your code the field is called for example SelectedServerPOP and the notification goes by selectedServerPOP