Chris Milburn
Chris Milburn

Reputation: 919

Basic WPF validation

Im just getting started with WPF and particularly validation of entries in a TextBox.

This is my 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>

This is my binding code :-

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

and this is my backing code:-

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

Firstly if I select the textbox and tab out of it nothing happens, I would expect the "Description is Mandatory" exception to be thrown ? If I then enter something in the textbox and immediately delete it, then press tab the exception is thrown ?? This exception is then unhandled and I get an unhandled exception error.

Upvotes: 2

Views: 6121

Answers (2)

Chris Milburn
Chris Milburn

Reputation: 919

After much digging around the net and reading of MS documentation I found what I wanted. What you can do is set the 'LostKeyboardFocus' attribute to point to some method of your own, e.g. Here when the textbox loses focus, the method 'TextBoxLostKeyboardFocus()' is called.

  <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>

Upvotes: 1

msarchet
msarchet

Reputation: 15242

First, you don't validate with exceptions, Exceptions are designed to indicate that something exceptional has happened in the program flow and to provide an error.

See Data Validation in 3.5 for a clear explination of how WPF validation works

Secondly, the reason that you aren't seeing it when you simply enter and leave the TextBox is that you aren't actually updating the value for the bound property. When you enter in a value and then delete it you are updating that value.

Upvotes: 2

Related Questions