Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 513
  • Last Modified:

Validation in WPF

Hi,

  In the attached little application I'm doing some tests with WPF Validation but it is not working well. The platform is Dot.Net 3.5, VS 2008. The application is attached to be easier to understand what is going on.

  Tha application is this:

  1 A field is binded to a TextBox
  2 Another TextBox is attached to the first one.

  Questions:

  1. When I start the application, TextBox is filled but the Validation do not run. Why?
  2. When I type in the second TypeBox, the first one TextBox is populated but Validatoin in the second TextBox do not run. Why?
  3. When I fill the value of the field (Data), TextBox is populated but Validation does not run. Why?
  4. I wish to make a button not enabled when a Validation is active, I mean, if there is some wrong data in my screen, Save button is disabled. How is it done?

  That attached file is in RAR format because Expertes is blocking XAML files. So, delete the ZIP format o file to reach the correct format.

  Thanks for any explanation.

  Marco Castro
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Globalization;
using System.ComponentModel;

namespace WpfBinding
{
  /// <summary>
  /// Interaction logic for Window1.xaml
  /// </summary>
  public partial class Window1 : Window, INotifyPropertyChanged
  {
    public Window1()
    {
      InitializeComponent();
      Data = new Random().Next(10000);
    }

    private int _data;
    public int Data
    {
      get { return _data; }
      set
      {
        _data = value;
        NotifyPropertyChanged("Data");
      }
    }

    private void button2_Click(object sender, RoutedEventArgs e)
    {
      Close();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
      Data = new Random().Next(10000);
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
      if (PropertyChanged != null)
      {
        PropertyChanged(this, new PropertyChangedEventArgs(info));
      }
    }

    private void window_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
    	if (e.LeftButton == MouseButtonState.Pressed)
        this.DragMove();
    }

    private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
    {
    	new Window2().Show();
    }
  }

  public class AgeRangeRule : ValidationRule
  {
    private int _min;
    private int _max;

    public AgeRangeRule()
    {
    }

    public int Min
    {
      get { return _min; }
      set { _min = value; }
    }

    public int Max
    {
      get { return _max; }
      set { _max = value; }
    }

    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
      int age = 0;

      try
      {
        if (((string)value).Length > 0)
          age = Int32.Parse((String)value);
      }
      catch (Exception e)
      {
        return new ValidationResult(false, "Illegal characters or " + e.Message);
      }

      if ((age < Min) || (age > Max))
      {
        return new ValidationResult(false,
          "Please enter an age in the range: " + Min + " - " + Max + ".");
      }
      else
      {
        return new ValidationResult(true, null);
      }
    }
  }

  [ValueConversion(typeof(bool), typeof(Visibility))]
  public class BoolToVisibility : IValueConverter
  {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
      if ((bool)value)
        return Visibility.Visible;
      else
        return Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
      if ((Visibility)value == Visibility.Visible)
        return true;
      else
        return false;
    }
  }
}

Open in new window

WpfBinding.rar.zip
0
MarcoCastro
Asked:
MarcoCastro
1 Solution
 
apeterCommented:
Hope this article gives you more clarity and required information on validation rules.,

 http://www.codeproject.com/KB/WPF/wpfvalidation.aspx
0
 
MarcoCastroAuthor Commented:
Well the quertion was not totally explained.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now