Wrote an application. Everything worked well, properties in the view binder to text fields with a view Text
Text= "{Binding PersonName}" Then I wanted to validate the input data. Validation works, data from the fields in the properties are not written.
<Window.Resources> <viewModels:RegistrationFormViewModel x:Key="RegistrationFormViewModel" /> <converters:BitmapToImageSourceConverter x:Key="BitmapToImageSourceConverter"/> <viewModels:RegistrationFormViewModel x:Key="NameInstance" PersonName="" /> <viewModels:RegistrationFormViewModel x:Key="LastNameInstance" PersonLastName="" /> <viewModels:RegistrationFormViewModel x:Key="DepartmentInstance" PersonDepartment="" /> <!-- IData Error style --> <Style x:Key="TextErrorStyle" TargetType="{x:Type TextBox}"> <Setter Property="Validation.ErrorTemplate"> <Setter.Value> <ControlTemplate x:Name="TextErrorTemplate"> <DockPanel> <Border BorderBrush="Red" BorderThickness="2"> <AdornedElementPlaceholder/> </Border> <TextBlock FontSize="20" Foreground="Red">*!*</TextBlock> </DockPanel> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="Validation.HasError" Value="True"> <!--<Setter Property="Background" Value="Red"/>--> <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self},Path=(Validation.Errors)[0].ErrorContent}"></Setter> </Trigger> </Style.Triggers> </Style> </Window.Resources> Text box
<TextBox Background="Transparent" BorderBrush="Black" Height="30" Width="140" Style="{StaticResource TextErrorStyle}" TextAlignment="Center" Foreground="White" HorizontalAlignment="Center"> <TextBox.Text> <Binding Path="PersonName" Source="{StaticResource NameInstance}" ValidatesOnDataErrors="True" UpdateSourceTrigger="PropertyChanged"> <Binding.ValidationRules> <ExceptionValidationRule></ExceptionValidationRule> </Binding.ValidationRules> </Binding> </TextBox.Text> <TextBox.ToolTip> <ToolTip HasDropShadow="True" Content="{Binding EnterPersonName}" /> </TextBox.ToolTip> </TextBox> A piece of twist model
public class RegistrationFormViewModel : BaseViewModel, IDataErrorInfo { #region TextBox properties /// <summary> /// Binding person name to TextBox /// </summary> [Required(AllowEmptyStrings = false)] public string PersonName { get; set; } /// <summary> /// Binding person last name TextBox /// </summary> [Required(AllowEmptyStrings = false)] public string PersonLastName { get; set; } /// <summary> /// Binding person department TextBox /// </summary> [Required(AllowEmptyStrings = false)] public string PersonDepartment { get; set; } /// <summary> /// Error indexer /// </summary> /// <param name="columnName"></param> /// <returns></returns> public string this[string columnName] { get { string error = String.Empty; switch (columnName) { case "PersonName": if (PersonName == null || PersonName == "") { error = "Введите имя!"; } break; case "PersonLastName": if (PersonLastName == null || PersonLastName == "") { error = "Введите Фамилию!"; } break; case "PersonDepartment": if (PersonDepartment == null || PersonDepartment == "") { error = "Введите название отдела!"; } break; } return error; } } /// <summary> /// Error exception throwing /// </summary> public string Error => "Введите данные!"; Base Observer Class
/// <summary> /// Class for view notification /// </summary> public abstract class NotificationObject : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } protected void SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null) { if (!EqualityComparer<T>.Default.Equals(field, value)) { field = value; OnPropertyChanged(propertyName); } } }
Source="{StaticResource NameInstance}"- tym32167 8:52 pmText= "{Binding PersonName}", so I assumed that you do not have any work for you since the expression for binding you have anotherSource="{StaticResource NameInstance}"- tym32167