如何从C#ValidationRule访问WPF XAML GUI元素?

时间:2018-07-22 05:46:18

标签: c# wpf validation xaml

我有一个WPF GUI,其中两个字段分别名为“一个”和“两个”。

“一个”的值必须小于“两个”的值。这由称为ValidateOne的ValidationRule进行测试。

“二”的值必须大于“一”的值。这由称为ValidateTwo的ValidationRule进行测试。

任何人都可以告诉我如何从C#ValidationRule访问XAML GUI元素以便进行验证吗?

我知道我需要用引用名为one和2的GUI元素的东西替换“ MainWindow.one”和“ MainWindow.two”。我还没找到怎么做的。

我已附上以下代码。

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);
            }
        }
    }
}

有人可以帮忙吗?

我使用Mark的参考文献Pass parameter to ValidationRule classWPF: Passing a Data Bound Value To a Validation Rule解决了这个问题,我发现它们对参考2,000 Things You Should Know About WPF最为有用,以了解依赖项属性。

0 个答案:

没有答案