Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have made a couple of assumptions since I am not sure I understand you code completely but I think you could consider possibly implementing a custom validation rule. First off, since your custom ValidationRule will take care of the validation you could get the logic out of your model class's property definition and "dumb down" your poco:</p> <pre><code>class PropertyStorageContainer { public string Property { get; set; } } </code></pre> <p>It seems you desire your view model to act as a basic wrapper around your model class. Again, I will assume this is valid based on the description of your scenario:</p> <pre><code>class PropertyStorageContainerViewModel : INotifyPropertyChanged { private PropertyStorageContainer model; public PropertyStorageContainerViewModel(PropertyStorageContainer model) { this.model = model; } public string Property { get { if (model != null) { return model.Property; } else { return null; } } set { if (model.Property != value) { model.Property = value; RaisePropertyChanged("Property"); } } } // INotifyPropertyChanged implementation... } </code></pre> <p>Now create a new class that extends System.Windows.Controls.ValidationRule and override the abstract Validate method in order implement your validation logic. For the example, I created a rule that just checks if the string is null or empty (assuming that would be an invalid scenario):</p> <pre><code>class IsNullOrEmptyValidationRule : ValidationRule { public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo) { string s = (value ?? string.Empty).ToString(); if (string.IsNullOrEmpty(s)) { // Invalid... return new ValidationResult(false, "Please enter a value."); } else { // Valid... return new ValidationResult(true, null); } } } </code></pre> <p>Now for the XAML... Here is an example of a TextBox that adds the validation rule to its binding validation rules (can be multiple rules).</p> <pre><code> &lt;TextBox Name="textBox1" Width="50" FontSize="12" Validation.ErrorTemplate="{StaticResource validationTemplate}" Style="{StaticResource textBoxInError}"&gt; &lt;TextBox.Text&gt; &lt;Binding Path="Property" UpdateSourceTrigger="PropertyChanged" &gt; &lt;Binding.ValidationRules&gt; &lt;local:IsNullOrEmptyValidationRule /&gt; &lt;/Binding.ValidationRules&gt; &lt;/Binding&gt; &lt;/TextBox.Text&gt; &lt;/TextBox&gt; </code></pre> <p>Then define the following resources (referenced above) somewhere (e.g., Window.Resources). First a ControlTemplate to define how the TextBox should look when in invalid state:</p> <pre><code>&lt;ControlTemplate x:Key="validationTemplate"&gt; &lt;DockPanel&gt; &lt;TextBlock Foreground="Red" FontSize="15" Text="!!!" /&gt; &lt;AdornedElementPlaceholder/&gt; &lt;/DockPanel&gt; &lt;/ControlTemplate&gt; </code></pre> <p>Additionally you could define a style trigger to display the error message. Here I just bind it to the ToolTip property of the TextBox:</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>
 

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