How do I make validation rules work with two controls, Part 2 of 2

Rod At Work 866 Reputation points
2020-09-14T18:00:16.117+00:00

(Part 1 is here: https://learn.microsoft.com/en-us/answers/questions/94723/how-do-i-make-validation-rules-work-with-two-contr.html)

I'm not sure this will help, but here's the code to the validation rule class I've defined:

using System;  
using System.Globalization;  
using System.Windows.Controls;  
  
namespace CoreFramework.ViewModel.ValidationRules  
{  
    public class MaxProficienciesValidationRule : ValidationRule  
    {  
        public long Min { get; set; }  
  
        public long SolutionID { get; set; }  
  
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)  
        {  
            long proposedMaxProficiencies = 0;  
  
            if (SolutionID > 0)  
            {  
                return ValidationResult.ValidResult;  
            }  
  
            try  
            {  
                if (((string)value).Length > 0)  
                {  
                    proposedMaxProficiencies = Int64.Parse((string)value);  
                }  
                else  
                {  
                    return new ValidationResult(false, "Empty string invalid");  
                }  
            }  
            catch (Exception ex)  
            {  
                return new ValidationResult(false, $"Illegal character(s) or {ex.Message}");  
            }  
  
            if (proposedMaxProficiencies < Min)  
            {  
                return new ValidationResult(false, "MaxProficiencies must be greater than 0");  
            }  
  
            return ValidationResult.ValidResult;  
        }  
    }  
}  

I'm sure I'm going to have to define a converter class that will take the ValidationResult and converter it to a Thickness, but I'm not sure where to do that within the XAML.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,697 questions
{count} vote

1 answer

Sort by: Most helpful
  1. DaisyTian-1203 11,616 Reputation points
    2020-09-21T01:25:44.787+00:00

    Here is the code for demo.
    Part 1: Code for MainWindow.xaml(ps: code for the picture which i can't paste here)
    26021-capture.png

         <StackPanel>  
                <Border>  
                    <TextBox x:Name="MaxProficienciesTextBox"  
                             Margin="0,5"  
                             MaxLength="2"  
                             >  
                        <TextBox.Text>  
                            <Binding Path="MaxProficiencies" UpdateSourceTrigger="PropertyChanged">  
                                <Binding.ValidationRules>  
                                    <!--<local:MaxProficienciesValidationRule Min="1" SolutionID="{Binding SolutionID}" />-->  
                                    <local:MaxProficienciesValidationRule Min="2" SolutionID="0" />  
                                </Binding.ValidationRules>  
                            </Binding>  
                        </TextBox.Text>  
                    </TextBox>  
                </Border>  
            </StackPanel>  
    

    Part 2 : Code for MainWindow.xaml.cs

     public partial class MainWindow : Window  
        {  
            MyModel myModel = new MyModel();  
            public MainWindow()  
            {  
                InitializeComponent();  
                this.DataContext = myModel;  
            }        
        }  
        
        public class MaxProficienciesValidationRule : ValidationRule  
        {  
            public long Min { get; set; }  
            public long SolutionID { get; set; }  
            public override ValidationResult Validate(object value, CultureInfo cultureInfo)  
            {  
                long proposedMaxProficiencies = 0;  
                if (SolutionID > 0)  
                {  
                    return ValidationResult.ValidResult;  
                }  
                try  
                {  
                    if (((string)value).Length > 0)  
                    {  
                        proposedMaxProficiencies = Int64.Parse((string)value);  
                    }  
                    else  
                    {  
                        return new ValidationResult(false, "Empty string invalid");  
                    }  
                }  
                catch (Exception ex)  
                {  
                    return new ValidationResult(false, $"Illegal character(s) or {ex.Message}");  
                }  
      
                if (proposedMaxProficiencies < Min)  
                {  
                    return new ValidationResult(false, "MaxProficiencies must be greater than 0");  
                }  
      
                return ValidationResult.ValidResult;  
            }  
        }  
    

    Part 3: Code for MymModel.cs

    class MyModel : ViewModeBase  
        {  
            private long maxProficiencies;  
            public long MaxProficiencies  
            {  
                get { return maxProficiencies; }  
                set  
                {  
                    maxProficiencies = value;  
                    InvokePropertyChanged("MaxProficiencies");  
                }  
            }  
        }  
      
        class ViewModeBase : INotifyPropertyChanged  
        {  
      
            public event PropertyChangedEventHandler PropertyChanged;  
            protected void InvokePropertyChanged(string property)  
            {  
                if (this.PropertyChanged != null)  
                {  
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(property));  
                }  
            }  
        }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments