Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To show the error message in a tool tip put this into your Application.Resources:</p> <pre><code>&lt;Style x:Key="textBoxInError" TargetType="{x:Type TextBox}"&gt; &lt;Style.Triggers&gt; &lt;Trigger Property="Validation.HasError" Value="true"&gt; &lt;Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}"/&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; </code></pre> <p>( Example from <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.validation.errortemplate.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/system.windows.controls.validation.errortemplate.aspx</a> )</p> <p>To enable/disable a button you could use something along the line of</p> <pre><code>&lt;Button x:Name="btnOK" Content="OK" IsDefault="True" Click="btnOK_Click"&gt; &lt;Button.Style&gt; &lt;Style TargetType="{x:Type Button}"&gt; &lt;Setter Property="IsEnabled" Value="false" /&gt; &lt;Style.Triggers&gt; &lt;MultiDataTrigger&gt; &lt;MultiDataTrigger.Conditions&gt; &lt;Condition Binding="{Binding ElementName=txt1, Path=(Validation.HasError)}" Value="false" /&gt; &lt;Condition Binding="{Binding ElementName=txt2, Path=(Validation.HasError)}" Value="false" /&gt; &lt;/MultiDataTrigger.Conditions&gt; &lt;Setter Property="IsEnabled" Value="true" /&gt; &lt;/MultiDataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/Button.Style&gt; &lt;/Button&gt; </code></pre> <p>or you could implement ICommand and use command binding.</p> <p><strong>EDIT</strong></p> <p>Here is a fully working example. It displays a window with two TextBoxes. The Button is enabled if and only if both TextBoxes are non-empty. Create a project called ValidationDemo and put the following files in it:</p> <p><strong>MainWindow.xaml:</strong></p> <pre><code>&lt;Window x:Class="ValidationDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="146" Width="223"&gt; &lt;Window.Resources&gt; &lt;Style TargetType="{x:Type TextBox}"&gt; &lt;Style.Triggers&gt; &lt;Trigger Property="Validation.HasError" Value="true"&gt; &lt;Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}"/&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/Window.Resources&gt; &lt;Grid&gt; &lt;Label Content="A" Height="28" HorizontalAlignment="Left" Margin="46,7,0,0" Name="label1" VerticalAlignment="Top" /&gt; &lt;TextBox Name="txtA" Text="{Binding Path=TextA, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" Height="23" HorizontalAlignment="Left" Margin="69,12,0,0" VerticalAlignment="Top" Width="120" /&gt; &lt;Label Content="B" Height="28" HorizontalAlignment="Left" Margin="46,39,0,0" Name="label2" VerticalAlignment="Top" /&gt; &lt;TextBox Name="txtB" Text="{Binding Path=TextB, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" Height="23" HorizontalAlignment="Left" Margin="69,41,0,0" VerticalAlignment="Top" Width="120" /&gt; &lt;Button Name="btnOk" Content="OK" Height="23" HorizontalAlignment="Left" Margin="114,70,0,0" VerticalAlignment="Top" Width="75" Click="btnOk_Click"&gt; &lt;Button.Style&gt; &lt;Style TargetType="{x:Type Button}"&gt; &lt;Setter Property="IsEnabled" Value="false" /&gt; &lt;Style.Triggers&gt; &lt;MultiDataTrigger&gt; &lt;MultiDataTrigger.Conditions&gt; &lt;Condition Binding="{Binding ElementName=txtA, Path=(Validation.HasError)}" Value="false" /&gt; &lt;Condition Binding="{Binding ElementName=txtB, Path=(Validation.HasError)}" Value="false" /&gt; &lt;/MultiDataTrigger.Conditions&gt; &lt;Setter Property="IsEnabled" Value="true" /&gt; &lt;/MultiDataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/Button.Style&gt; &lt;/Button&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p><strong>MainWindow.xaml.cs:</strong></p> <pre><code>using System.Windows; namespace ValidationDemo { /// &lt;summary&gt; /// Interaction logic for MainWindow.xaml /// &lt;/summary&gt; public partial class MainWindow : Window { private Model model = new Model(); public MainWindow() { InitializeComponent(); this.DataContext = this.model; } private void btnOk_Click(object sender, RoutedEventArgs e) { Application.Current.Shutdown(); } } } </code></pre> <p><strong>Model.cs:</strong></p> <pre><code>using System; using System.ComponentModel; namespace ValidationDemo { public class Model : INotifyPropertyChanged, IDataErrorInfo { public event PropertyChangedEventHandler PropertyChanged; private string textA = string.Empty; public string TextA { get { return this.textA; } set { if (this.textA != value) { this.textA = value; this.OnPropertyChanged("TextA"); } } } private string textB = string.Empty; public string TextB { get { return this.textB; } set { if (this.textB != value) { this.textB = value; this.OnPropertyChanged("TextB"); } } } public string Error { get { throw new NotImplementedException(); } } public string this[string columnName] { get { string result = string.Empty; switch (columnName) { case "TextA": if (string.IsNullOrEmpty(this.textA)) { result = "'A' must not be empty"; } break; case "TextB": if (string.IsNullOrEmpty(this.textA)) { result = "'B' must not be empty"; } break; } return result; } } protected virtual void OnPropertyChanged(string propertyName) { if (this.PropertyChanged != null) { this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } } </code></pre>
 

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