c# - 我的第一个 C# WPF ValidationRule 没有触发

标签 c# wpf

我正在尝试学习如何实现数据验证,但我的第一次尝试并没有触发 lblSource_Error 事件;有谁知道我错过了什么?

我的窗口的 XAML:

<Window x:Class="cCompleteWPFResourcesExamples.wValidationRule"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:cCompleteWPFResourcesExamples"
    Title="wValidationRule" Height="300" Width="300">
<Window.Resources>
    <local:Customer x:Key="rCustomer" Forename="InXaml" Surname="Created" ID="1"     
AmountOutstanding="0"/>
</Window.Resources>
<StackPanel x:Name="stkMain" DataContext="{StaticResource rCustomer}">
    <Label x:Name="lblSource" Validation.Error="lblSource_Error">
        <Label.Content>
            <Binding Path="ID" NotifyOnValidationError="True">
                <Binding.ValidationRules>
                    <local:cIDValidationRule/>
                </Binding.ValidationRules>
            </Binding>
        </Label.Content>
    </Label>
    <Label x:Name="lblErrorMessage" Content="No Error Yet"/>
</StackPanel>
</Window>

我的窗口的代码:

   namespace cCompleteWPFResourcesExamples
    {
    /// <summary>
    /// Interaction logic for wValidationRule.xaml
    /// </summary>
    public partial class wValidationRule : Window
    {
        Customer cus = new Customer();

        public wValidationRule()
        {
            InitializeComponent();
            cus.ID = 0;
            stkMain.DataContext = cus;
        }


        private void lblSource_Error(object sender, ValidationErrorEventArgs e)
        {
            lblErrorMessage.Content = e.Error.ErrorContent.ToString();
        }
    }
    }

我的验证规则:

using System.Windows.Controls;

namespace cCompleteWPFResourcesExamples 
{
public class cIDValidationRule : ValidationRule
{
    public override ValidationResult Validate(object value, 
System.Globalization.CultureInfo cultureInfo)
    {
        int iValue = (int)value;
        if (iValue == 0) return new ValidationResult(false, "No ID number");

        return new ValidationResult(true, null);
    }
}
}

Customer 对象非常简单:只有几个属性。

谢谢!

詹姆斯

最佳答案

Awww 这么悲伤的标题 :) :) 首先 wpf validationrule 没有做你想做的事。

每次将输入值(绑定(bind)目标属性值)传输到绑定(bind)源属性时,绑定(bind)引擎都会检查与绑定(bind)关联的每个 ValidationRule。

记住这一点:

您键入一些内容,该值将保存到源 => ValidationRule 将触发。

您想在 Label 中显示一些内容,并且值正在从源传输到 Label => ValidationRule 永远不会触发。

如果您希望您的示例能够正常工作,那么取而代之的是 TextBox 并将绑定(bind)模式设置为 TwoWay,这样您就可以输入一些内容,并且绑定(bind)会将输入的值保存到源中,从而导致 ValidationRule 触发。 :)

关于c# - 我的第一个 C# WPF ValidationRule 没有触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19836868/

相关文章:

C# BackgroundWorker 并行调用 DoWorkEventHandler

c# - "Run as administrator"选项没有出现在资源管理器的上下文菜单中

.net - 将 WPF(需要 STAThread)与无法与 STAThread 配合使用的 API 结合使用

wpf - WPF在不同线程中创建多个窗口

C# Web 应用程序问题

c# - MySql 的异常 : Variable 'character_set_client' can't be set to the value of 'utf16'

c# - 向用户发送消息时的 PEER_ID_INVALID

c# - 为什么我的 WPF 应用程序会在我碰撞鼠标滚轮时崩溃?

c# - 自动完成导致文本向右滚动

c# - MVVM/NHibernate - 如何进行动态模型验证?