基本WPF验证

本文关键字:验证 WPF 基本 | 更新日期: 2023-09-27 18:20:04

我刚刚开始使用WPF,尤其是验证TextBox中的条目。

这是我的XAML:-

<Window x:Class="WpfTestBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" 
        Height="350" 
        Width="525"
        Loaded="OnInit">
    <Grid>
        <Button Content="OK" Height="23" HorizontalAlignment="Left" Margin="235,164,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="206,108,0,0"                  
                 Name="textBox1" VerticalAlignment="Top" Width="120" >
            <TextBox.Text>
                <Binding Path="Description" UpdateSourceTrigger="LostFocus" >
                    <Binding.ValidationRules>
                        <ExceptionValidationRule />
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>
    </Grid>
</Window>

这是我的绑定代码:-

namespace WpfTestBinding
{
    class MyDataItem
    {
        private String _description;
        public String Description
        {
            get { return _description; }
            set
            {
                _description = value;
                Debug.WriteLine("Setting description ="+value);
                if (String.IsNullOrEmpty( value ))
                {
                    throw new ApplicationException("Description is mandatory.");
                }                                
            }
        }
    }
}

这是我的支持代码:-

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
    namespace WpfTestBinding
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
            private void OnInit(object sender, RoutedEventArgs e)
            {
                this.DataContext = new MyDataItem();
            }
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                Debug.WriteLine("Button clicked.");
            }
        }
    }

首先,如果我选择文本框并从中选择标签,什么都没发生,我会期望抛出"Description is Mandatory"异常吗?如果我在文本框中输入一些内容并立即将其删除,然后按tab键,则会引发异常??此异常随后未处理,我得到一个未处理的异常错误。

基本WPF验证

首先,您不使用异常进行验证,异常用于指示程序流中发生了异常并提供错误。

请参阅3.5中的数据验证,以清楚地解释WPF验证如何在中工作

其次,当您简单地输入和离开TextBox时,您没有看到它的原因是您实际上没有更新绑定属性的值。当您输入一个值然后将其删除时,您正在更新该值。

经过大量的网络挖掘和阅读MS文档,我找到了我想要的东西。您可以将"LostKeyboardFocus"属性设置为指向您自己的某个方法,例如,当文本框失去焦点时,会调用方法"TextBoxLostKeyboard focus()"。

  <TextBox Height="23" HorizontalAlignment="Left" Margin="206,108,0,0"                  
           Name="textBox1" 
           LostKeyboardFocus="TextBoxLostKeyboardFocus" 
           VerticalAlignment="Top" Width="120" >
      <TextBox.Text>
          <Binding Path="Description" 
                         UpdateSourceTrigger="LostFocus" 
                         ValidatesOnDataErrors="True" 
                         NotifyOnSourceUpdated="True"                         
                         Mode="TwoWay">
           </Binding>
      </TextBox.Text>
  </TextBox>