Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>two weeks ago, I was facing the same issue.</p> <p>I came up with a solution, after seeing this blog by Josh Smith.</p> <p><a href="http://joshsmithonwpf.wordpress.com/2008/10/08/binding-to-validationerrors0-without-creating-debug-spew/" rel="noreferrer">http://joshsmithonwpf.wordpress.com/2008/10/08/binding-to-validationerrors0-without-creating-debug-spew/</a></p> <p>The trick is to define a DataTemplate to render the ValidationError object and then use a ContentPresenter to display the error message. If there is no error, then the ContentPresenter will not be displayed. </p> <p>Below, I have shared the code of the sample app that I created. </p> <p><img src="https://i.stack.imgur.com/q1Aeu.png" alt="Without errors"> <img src="https://i.stack.imgur.com/uGsZk.png" alt="With errors"></p> <p>Here is the XAML:</p> <pre><code>&lt;Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" SizeToContent="WidthAndHeight" Title="MainWindow"&gt; &lt;StackPanel Margin="5"&gt; &lt;StackPanel.Resources&gt; &lt;DataTemplate DataType="{x:Type ValidationError}"&gt; &lt;TextBlock Text="{Binding ErrorContent}" Foreground="White" Background="Red" VerticalAlignment="Center" FontWeight="Bold"/&gt; &lt;/DataTemplate&gt; &lt;/StackPanel.Resources&gt; &lt;TextBox Name="TextBox1" Text="{Binding Text1, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"/&gt; &lt;ContentPresenter Content="{Binding ElementName= TextBox1, Path=(Validation.Errors).CurrentItem}" HorizontalAlignment="Left"/&gt; &lt;TextBox Name="TextBox2" Text="{Binding Text2, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"/&gt; &lt;ContentPresenter Content="{Binding ElementName= TextBox2, Path=(Validation.Errors).CurrentItem}" HorizontalAlignment="Left"/&gt; &lt;Button Content="Validate" Click="Button_Click"/&gt; &lt;/StackPanel&gt; &lt;/Window&gt; </code></pre> <p>The code behind file:</p> <pre><code>namespace WpfApplication1 { /// &lt;summary&gt; /// Interaction logic for MainWindow.xaml /// &lt;/summary&gt; public partial class MainWindow : Window { private ViewModel _ViewModel = null; public MainWindow() { InitializeComponent(); _ViewModel = new ViewModel(); DataContext = _ViewModel; } private void Button_Click(object sender, RoutedEventArgs e) { _ViewModel.Validate = true; _ViewModel.OnPropertyChanged("Text1"); _ViewModel.OnPropertyChanged("Text2"); } } } </code></pre> <p>The ViewModel:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; namespace WpfApplication1 { public class ViewModel : INotifyPropertyChanged, IDataErrorInfo { private string _Text1; public string Text1 { get { return _Text1; } set { _Text1 = value; OnPropertyChanged("Text1"); } } private string _Text2; public string Text2 { get { return _Text2; } set { _Text2 = value; OnPropertyChanged("Text2"); } } public bool Validate { get; set; } #region INotifyPropertyChanged Implemenation public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } #endregion #region IDataErrorInfo Implementation public string Error { get { return null; } } public string this[string columnName] { get { string errorMessage = string.Empty; if (Validate) { switch (columnName) { case "Text1": if (Text1 == null) errorMessage = "Text1 is mandatory."; else if (Text1.Trim() == string.Empty) errorMessage = "Text1 is not valid."; break; case "Text2": if (Text2 == null) errorMessage = "Text2 is mandatory."; else if (Text2.Trim() == string.Empty) errorMessage = "Text2 is not valid."; break; } } return errorMessage; } } #endregion } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload