最后一个文本框的更改将使用相同的值更新所有文本框

Change in last Textbox will update all textboxes with the same value

我使用 Gerald Gomes CodeProject 文章向我的 WPF 表单添加了一些验证 http://www.codeproject.com/Tips/690130/Simple-Validation-in-WPF 使用这个我遇到了一个奇怪的问题!

我有这个xaml

<Window x:Class="TestValidate.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TestValidate"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ControlTemplate x:Key="validationErrorTemplate">
            <DockPanel>
                <StackPanel Orientation="Horizontal" DockPanel.Dock="Top">
                    <Grid Width="12" Height="12">
                        <Ellipse Width="12" Height="12" Fill="Red" HorizontalAlignment="Center" VerticalAlignment="Center"

                             ></Ellipse>
                        <TextBlock Foreground="White" FontWeight="Heavy" FontSize="8" HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center"
                               ToolTip="{Binding ElementName=ErrorAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"
                               >X</TextBlock>
                    </Grid>
                    <TextBlock Foreground="Red" FontWeight="12" Margin="2,0,0,0"
                           Text="{Binding ElementName=ErrorAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"
                           ></TextBlock>
                </StackPanel>
                <AdornedElementPlaceholder x:Name="ErrorAdorner" />
            </DockPanel>
        </ControlTemplate>
    </Window.Resources>
    <Grid RenderTransformOrigin="0.519,0.497">
        <TextBlock HorizontalAlignment="Left" Margin="36,80,0,0" TextWrapping="Wrap" Text="Number" VerticalAlignment="Top" Width="79" />
        <TextBox x:Name="MaxMessageListCount" HorizontalAlignment="Left" Height="23" Margin="120,73,0,0" TextWrapping="Wrap" VerticalAlignment="Top"
                 Width="156" Validation.ErrorTemplate="{StaticResource validationErrorTemplate}" TabIndex="4" ToolTip="Max number of messages that the list will contain before it clears">
            <TextBox.Text>
                <Binding Path="Name" Mode="TwoWay" UpdateSourceTrigger="LostFocus">
                    <Binding.ValidationRules>
                        <local:NumberValidator />
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>

        <TextBlock HorizontalAlignment="Left" Margin="36,126,0,0" TextWrapping="Wrap" Text="Text" VerticalAlignment="Top" Width="79" />
        <TextBox x:Name="ClientId" HorizontalAlignment="Left" Height="23" Margin="120,119,0,0" TextWrapping="Wrap" VerticalAlignment="Top"
                 Width="156"  Validation.ErrorTemplate="{StaticResource validationErrorTemplate}" TabIndex="5" ToolTip="Client Id MUST be unique for the cell">
            <TextBox.Text>
                <Binding Path="Name" Mode="TwoWay" UpdateSourceTrigger="LostFocus">
                    <Binding.ValidationRules>
                        <local:EmptyValidator />
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>
    </Grid>
</Window>

以及这些验证规则

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace TestValidate
{
    public class EmptyValidator : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            if (value == null)
                return new ValidationResult(false, "value can't be empty");
            else
            {
                string sValue = value.ToString();

                if (string.IsNullOrWhiteSpace(sValue))
                    return new ValidationResult(false, "Value can't be empty");
            }
            return ValidationResult.ValidResult;
        }
    }

    public class NumberValidator : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            if (value == null)
                return new ValidationResult(false, "value can't be empty");
            else
            {
                if (!IsNumeric(value))
                    return new ValidationResult(false, "value must be a number");
            }
            return ValidationResult.ValidResult;
        }

        private bool IsNumeric(object value)
        {
            bool result = true;
            string number = value.ToString();
            int num = -1;
            result = int.TryParse(number, out num);

            return result;
        }
    }
}

如果我更改最后一个文本框 (x:Name="ClientId") 的值,则输入的值也将填充到第一个文本框 (x:Name="MaxMessageListCount")

我做错了什么?

整个项目可以从这里下载 http://andis59.se/wpf/TestValidate.zip

// 安德斯

您将两个文本框绑定到 'Name' 属性,

<Binding Path="Name" Mode="TwoWay" UpdateSourceTrigger="LostFocus">

例如,在您的情况下,我会将 ClientId TextBox 绑定到

<Binding Path="ClientId" Mode="TwoWay" UpdateSourceTrigger="LostFocus">