Я пишу приложение WPF с подходом MVVM и использую IDataErrorInfo для проверки ошибок.Когда я загружаю представление, проверка проверяется без изменения содержимого TextBox, я решил эту проблему следующим образом.
Как подавить проверку, когда ничего не введено
Но теперь проблема в том, что я не могу реализовать 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, но при загрузке данных отображается ошибка загрузки.
, поэтому я хочу, чтобы яхотите отключить проверку при загрузке, а также обновить содержимое после завершения процесса.
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;
}
}