1
Answer

Type validation on DataGrid and TextBox forced on and cannot be disabled in .net 4

Photo of Andrew Lewis

Andrew Lewis

14y
3k
1

We are in the process of converting a .NET 3.5sp1 WPF application to .NET 4 and have noticed a change in behavior. It seems like type validation on DataGrid and TextBox is now defaulted to "on", and furthermore it cannot be disabled even by setting ValidatesOnExceptions in the Binding to false. This is different from the behavior in 3.5sp1 and no documentation on .NET 4 makes any reference to this change in behavior. Has anyone seen this? Is it intentional behavior, and can it be turn off?

For example, binding an integer to a textbox and trying to enter a string will trigger a validation error, a change over .net3.5sp1 where you would have to first enable validation. The following code shows the issues, changing the framework from 3.5sp1 to 4 changes the behavior of the textbox, in 4 type validation is performed.

 

XAML
<Window x:Class="BindValidation.MainWindow"
xmlns="
http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="
http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:BindValidation"
Title="Binding Validation Test"
ResizeMode="NoResize" Width="200" Height="100" WindowStartupLocation="CenterScreen" Background="Gainsboro">

<Window.Resources>
<!-- Binding Data Source for TextBox -->
 <c:MyDataSource x:Key="data"/>
</Window.Resources>

<Grid>
 <TextBox Name="textBox1" Width="80" Height="20" Grid.Row="0" Grid.Column="1" Margin="2">
  <TextBox.Text>
   <Binding Path="Age" Source="{StaticResource data}" UpdateSourceTrigger="PropertyChanged" />
  </TextBox.Text>
 </TextBox>
</Grid>
</Window>

Code Behind:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;


namespace BindValidation
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public class MyDataSource
    {
        private int _age;

        public int Age
        {
            get { return _age; }
            set { _age = value; }
        }
    }

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

Answers (1)