Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit DataGrid and BindingValidationError
1 replies. Latest Post by elmore.adam on July 4, 2009.
(0)
VeroToad
Member
0 points
1 Posts
07-02-2009 5:26 AM |
Hi Guys!
I need to validate user-entry data in a Datagrid with customized DataTemplate (with textbox). On the web, I've found a lot of examples (like http://msdn.microsoft.com/en-us/library/cc278072(VS.95).aspx or http://weblogs.asp.net/manishdalal/archive/2008/08/28/silverlight-business-application-part-3-validation-sync.aspx) that suggests to change textbox properties NotifyOnValidationError and ValidatesOnExceptions, setting them "true". So, the GridView's event "BindingValidationError" could be directly managed.
This is my sample code:
Page.xaml1 <UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" x:Class="SilverlightApplication1.Page"2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Width="400" Height="300">5 <Grid x:Name="LayoutRoot" Background="White">6 <data:DataGrid x:Name="MyGrid" AutoGenerateColumns="False" Grid.Row="1" >7 <data:DataGrid.Columns>8 <data:DataGridTemplateColumn Header="Valore">9 <data:DataGridTemplateColumn.CellTemplate>10 <DataTemplate>11 <TextBox Text="{Binding Path=Valore,Mode=TwoWay,NotifyOnValidationError=true,ValidatesOnExceptions=true}" />12 </DataTemplate>13 </data:DataGridTemplateColumn.CellTemplate>14 </data:DataGridTemplateColumn>15 </data:DataGrid.Columns>16 </data:DataGrid>17 </Grid>18 </UserControl>
Page.xaml.cs1 using System;2 using System.Collections.Generic;3 using System.Linq;4 using System.Net;5 using System.Windows;6 using System.Windows.Controls;7 using System.Windows.Documents;8 using System.Windows.Input;9 using System.Windows.Media;10 using System.Windows.Media.Animation;11 using System.Windows.Shapes;12 13 namespace SilverlightApplication114 {15 public partial class Page : UserControl16 {17 public Page()18 {19 InitializeComponent();20 21 this.MyGrid.BindingValidationError += new EventHandler(MyGrid_BindingValidationError);22 this.Loaded += new RoutedEventHandler(Page_Loaded);23 }24 25 void Page_Loaded(object sender, RoutedEventArgs e)26 {27 MyGrid.ItemsSource = SilverlightApplication1.Valori.ValoriEsempio();28 29 }30 31 void MyGrid_BindingValidationError(object sender, ValidationErrorEventArgs e)32 {33 MessageBox.Show("Errore");34 }35 }36 }
MyClass.cs1 using System;2 using System.Net;3 using System.Windows;4 using System.Windows.Controls;5 using System.Windows.Documents;
6 using System.Windows.Ink;7 using System.Windows.Input;8 using System.Windows.Media;9 using System.Windows.Media.Animation;10 using System.Windows.Shapes;11 using System.ComponentModel;12 using System.Collections.ObjectModel;13 14 namespace SilverlightApplication115 {16 public class MyClass : INotifyPropertyChanged17 {18 protected int _Valore;19 public int Valore20 {21 get { return _Valore; }22 set23 {24 if (value == _Valore) return;25 if (value < 0 || value > 200)26 {27 throw new Exception("Valore non nel range specificato");28 }29 _Valore = value;30 OnPropertyChanged("Valore");31 }32 }33 34 #region INotifyPropertyChanged Membri di35 36 protected void OnPropertyChanged(string name)37 {38 if (PropertyChanged != null)39 PropertyChanged(this, new PropertyChangedEventArgs(name));40 }41 public event PropertyChangedEventHandler PropertyChanged;42 43 #endregion44 }45 46 47 public class Valori : ObservableCollection48 {49 public static Valori ValoriEsempio()50 {51 return new Valori() {52 new MyClass() { Valore=30},53 new MyClass() { Valore=40},54 new MyClass() { Valore=50},55 new MyClass() { Valore=20},56 new MyClass() { Valore=10},57 };58 }59 }60 }
When I change the textbox to a out-of-range value, MyGrid_BindingValidationError isn't executed. Why?Thank you very much
--Simone Viganò AKA VeroToad - MVP Windows Desktop Experience My blog: http://blogs.dotnethell.it/VeroToad RIO (Risorse in italiano per utenti di Office): http://www.riolab.org
elmore.adam
482 points
67 Posts
07-04-2009 2:03 PM |
Your code doesn't compile. First, replace Line 21 of Page.xaml.cs with the following:
this.MyGrid.BindingValidationError
+= new EventHandler<ValidationErrorEventArgs>(MyGrid_BindingValidationError);