I'm writing a timer application,and my program logic as below
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;
namespace Timer
{
/// <summary>
/// Interaction logic for Mainwindow.xaml
/// </summary>
public partial class MainWindow : Window,System.ComponentModel.INotifyPropertyChanged
{
enum State {Start,Stop,Reset }
State _timrState;
System.Timers.Timer secTimer;
int _sec;
int _min;
int _ahr;
public MainWindow()
{
InitializeComponent();
secTimer = new System.Timers.Timer(1000); //every sec
secTimer.Elapsed += new System.Timers.ElapsedEventHandler(secTimer_Elapsed);
_sec = 0;
_min = 0;
_ahr = 0;
DataContext = this;
}
void secTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Sec++;
}
public int Sec
{
get { return _sec; }
set { _sec = value; onpropertychanged("Sec"); }
}
public int Min
{
get { return _min; }
set { _min = value; onpropertychanged("Min"); }
}
public int Hour
{
get { return _ahr; }
set { _ahr = value; onpropertychanged("Hour"); }
}
private void stoptbut_Click(object sender, RoutedEventArgs e)
{
TimersState(State.Stop);
}
private void startBut_Click(object sender, RoutedEventArgs e)
{
TimersState(State.Start);
}
private void resetButt_Click(object sender, RoutedEventArgs e)
{
TimersState(State.Reset);
}
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
void onpropertychanged(string probName)
{
if (PropertyChanged != null)
PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(probName));
}
void TimersState(State st)
{
switch (st)
{
case State.Start:
StartTimers();
break;
case State.Stop :
StopTimers();
break;
case State.Reset:
StopTimers();
StartTimers();
break;
}
}
void StartTimers()
{
secTimer.Start();
}
void StopTimers()
{
secTimer.Stop();
Sec = 0;
Min = 0;
Hour = 0;
}
private void secTxtBox_Error(object sender, ValidationerrorEventArgs e)
{
if (e.Action == ValidationerrorEventAction.Added)
Sec = 1;
}
}
}
and i have bind the text property of a Textbox with the property "Sec" and created a validation rule as below
<TextBox x:Name="secTxtBox" Grid.Row="1" Grid.Column="2" Margin="10,2" VerticalAlignment="Center" Validation.Error="secTxtBox_Error" >
<TextBox.Text>
<Binding Path="Sec" Mode="TwoWay" ValidatesOnDataErrors="True" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<local:SecTimerValidation Check="Sec"></local:SecTimerValidation>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
and my validation class as below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
namespace Timer
{
public enum CheckFor { Sec, Min, Hour }
class SecTimerValidation : ValidationRule
{
public CheckFor Check { get; set; }
int _secval;
ValidationResult valRes ;
public SecTimerValidation()
{
_secval = 0;
valRes = null;
}
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
switch(Check)
{
case CheckFor.Sec:
_secval=Int32.Parse( value.ToString());
if (_secval > 60)
valRes = new ValidationResult(false, "Seconds error");
else
valRes = new ValidationResult(true,null);
break;
}
return valRes;
}
}
}
my problem is when the counter exceeds the value 60 it continue to increment and the validation not checked and when i steped through the program i found that the validation not executed at all,
if any one tell me why i will be appreciate

New Topic/Question
Reply




MultiQuote




|