我正在使用MVVM方法编写WPF应用程序,并且正在使用IDataErrorInfo进行错误验证。 当我加载View时,在不更改TextBox内容的情况下检查了验证,我通过遵循。
How to suppress validation when nothing is entered
但是现在的问题是,文本框中发生更改时,我无法实现INotifyPropertyChanged。
所以基本上我的工具有一个开始按钮,文本框和结束按钮。该应用程序执行以下操作
1) The start button onclick start a timer and disable the start button
2) User provide data in textbox
3) End the timer and calulate time difference. enable the start button for the next entry.
4) The problem is here the textbox data is not refreshed while I click the startbutton again. Since the onpropertychanged is not set on the property I can't able to refresh the data.
我可以通过实现onpropertychange来解决刷新文本框的问题,但是在加载数据时会显示onload错误。
所以我想要的是我想禁用onload验证,并在完成该过程后刷新内容。
Window.xaml
<Button IsEnabled="{Binding IsStartButtonEnabled}" Width="79" Height="19" Margin="-700,1,708.962,0" x:Name="startbutton" Command="{Binding AddNew}" Content="Start Time"/>
<Label Width="61" Height="24" Margin="-700,1,708.962,0" x:Name="StartTimeLabel" HorizontalAlignment="Left" Content="Start Time"/>
<TextBox IsEnabled="{Binding Isdisabled}" x:Name="StarttimeTextbox" Width="71" Height="24" Margin="-700,1,708.962,0" Text="{Binding ClaimNumber, Mode=TwoWay, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}"/>
<Button Command="{Binding stop }" Margin="500,0,0,0" Width="70" Height="20" VerticalAlignment="Center" IsEnabled="{Binding IsEnabledSubmit}" Content="Submit"/>
ViewModel.cs
private bool nameChanged = false;
private string claimnumber;
public string ClaimNumber
{
get { return this.claimnumber; }
set
{
this.claimnumber = value;
nameChanged = true;
//I have disabled the onpropertychanged because if I uncommented I can
//able to accomplish textbox refresh but the tool throws error when i
//load the data
// this.OnPropertyChanged("ClaimNumber");
}
}
AddNew = new RelayCommand(o => startbutton());
stop = new RelayCommand(o => stopbutton());
public void startbutton()
{
//The claimnumber should be empty whenever I click the startbutton
ClaimNumber = null;
stopWatch.Start();
dispatcherTimer.Start();
IsEnabled = true;
IsStartButtonEnabled = false;
}
public void stopbutton()
{
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}",
ts.Hours, ts.Minutes, ts.Seconds);
IsEnabled = false;
}
private string GetValidationError(string propertyName)
{
string errorMsg = null;
switch (propertyName)
{
case "ClaimNumber":
if ((nameChanged && propertyName.Equals("ClaimNumber")))
{
if (String.IsNullOrEmpty(this.claimnumber))
errorMsg = "Please provide the claimnumber";
else if (CheckInteger(claimnumber) == false)
errorMsg = "The given claimnumberis not a Number";
}
break;
}
}
答案 0 :(得分:0)
我已在停止按钮而不是开始按钮上将名称更改为boolean的值更改为false。
所以下面是我现在面临的问题,
1)应用程序将在加载应用程序时引发错误,该错误已通过移除属性的属性更改而解决。
2)使用的布尔名称已更改,而不是onpropertychaned。每当该属性被调用并使用布尔值触发错误时。
3)在“停止”按钮中,将所有属性设置为null,然后在其中使用onpropertytochanged来反映我在模型视图中执行的更改。
private bool nameChanged = false;
private string claimnumber;
public string ClaimNumber
{
get { return this.claimnumber; }
set
{
this.claimnumber = value;
nameChanged = true;
}
}
AddNew = new RelayCommand(o => startbutton());
stop = new RelayCommand(o => stopbutton());
public void startbutton()
{
stopWatch.Start();
dispatcherTimer.Start();
IsEnabled = true;
IsStartButtonEnabled = false;
}
public void stopbutton()
{
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}",
ts.Hours, ts.Minutes, ts.Seconds);
IsEnabled = false;
//The claimnumber made empty whenever i click end the button
ClaimNumber = null;
// Here i made the boolean to false so the error message will not be triggered.
nameChanged = false;
// Now the claimnumber which i emptied above should be reflected in the view
this.onPropertyChanged("Clainumber");
}
private string GetValidationError(string propertyName)
{
string errorMsg = null;
switch (propertyName)
{
case "ClaimNumber":
if ((nameChanged && propertyName.Equals("ClaimNumber")))
{
if (String.IsNullOrEmpty(this.claimnumber))
errorMsg = "Please provide the claimnumber";
else if (CheckInteger(claimnumber) == false)
errorMsg = "The given claimnumberis not a Number";
}
break;
}
}