Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

How to access WPF XAML GUI elements from a C# ValidationRule?

$
0
0

I have a WPF GUI with two fields named "one" and "two" respectively.

The value of "one" must be less than the value of "two". This is tested by the ValidationRule called ValidateOne.

The value of "two" must be greater than the value of "one". This is tested by the ValidationRule called ValidateTwo.

Can anyone please tell me how I can access the XAML GUI elements from my C# ValidationRule so that I can do the validation?

I know I need to replace "MainWindow.one" and "MainWindow.two" with something that references the GUI elements named one and two. I haven't been able to find how to do that.

I have attached the code below.

MainWindow.xaml

<Window x:Class="ValidateInput_Data.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ValidateInput_Data"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"><Grid><Grid.RowDefinitions><RowDefinition Height="10" /><RowDefinition Height="30" /><RowDefinition Height="10" /><RowDefinition Height="20" /></Grid.RowDefinitions><Grid Grid.Row="1" Background="AliceBlue" ShowGridLines="True"><Grid.ColumnDefinitions><ColumnDefinition Width=" 10" /><ColumnDefinition Width=" 40" /><ColumnDefinition Width=" 100" /><ColumnDefinition Width=" 40" /><ColumnDefinition Width=" 40" /><ColumnDefinition Width=" 100" /><ColumnDefinition Width="*" /><ColumnDefinition Width=" 100" /><ColumnDefinition Width=" 10" /></Grid.ColumnDefinitions><Label Content="One:" Grid.Column="1" /><TextBox x:Name="one" Grid.Column="2" Width="100" Height="23"
                     HorizontalAlignment="Left" VerticalAlignment="Center" TextWrapping="Wrap" ><TextBox.Text><Binding Path="BoundOne" Mode="TwoWay" NotifyOnValidationError="True" ValidatesOnExceptions="True"><Binding.ValidationRules><local:ValidateOne/></Binding.ValidationRules></Binding></TextBox.Text></TextBox><Label Content="Two:" Grid.Column="4" /><TextBox x:Name="two" Grid.Column="5" Width="100" Height="23"
                     HorizontalAlignment="Left" VerticalAlignment="Center" TextWrapping="Wrap"><TextBox.Text><Binding Path="BoundTwo" Mode="TwoWay" NotifyOnValidationError="True" ValidatesOnExceptions="True"><Binding.ValidationRules><local:ValidateTwo/></Binding.ValidationRules></Binding></TextBox.Text></TextBox></Grid></Grid></Window>

MainWindow.xaml.cs

using System;
using System.Windows;

namespace ValidateInput_Data
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public Int32 BoundOne { get; set; }
        public Int32 BoundTwo { get; set; }
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
        }
    }
}


ValidateInput.cs

using System;
using System.Globalization;
using System.Windows.Controls;


namespace ValidateInput_Data
{
    public partial class ValidateOne : ValidationRule
    {
        public Int32 InputNumberOne { get; set; }
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            try
            {
                InputNumberOne = Int32.Parse((string)value);
                if (InputNumberOne < MainWindow.two)                // Missing Reference Error is Here
                { return ValidationResult.ValidResult; }
                else
                {
                    return new ValidationResult(false, "Invalid input.");
                }
            }
            catch (Exception e)
            {
                return new ValidationResult(false, "Illegal characters have been entered or " + e.Message);
            }
        }
    }
    public partial class ValidateTwo : ValidationRule
    {
        public Int32 InputNumberTwo { get; set; }
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            try
            {
                InputNumberTwo = Int32.Parse((string)value);
                if (InputNumberTwo > MainWindow.one)                // Missing Reference Error is Here
                { return ValidationResult.ValidResult; }
                else
                {
                    return new ValidationResult(false, "Invalid input.");
                }
            }
            catch (Exception e)
            {
                return new ValidationResult(false, "Illegal characters have been entered or " + e.Message);
            }
        }
    }
}
Can anyone help?



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>