c# - 禁用验证 onload 并在属性上实现 INotifyPropertyChanged
问题描述
我正在使用 MVVM 方法编写 WPF 应用程序,并且正在使用 IDataErrorInfo 进行错误验证。当我加载视图时,在不更改文本框内容的情况下检查验证,我通过以下解决了这个问题。
但现在的问题是,当文本框发生变化时,我无法实现 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 错误。
所以我想要的是我想禁用加载验证并在完成该过程后刷新内容。
窗口.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"/>
视图模型.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;
}
}
解决方案
我在停止按钮而不是开始按钮上将 namechanged 布尔值更改为 false。
所以下面是我现在面临的问题已经解决了,
1) 应用程序将在应用程序加载时抛出错误,该错误已通过删除属性中的 onproperty 更改来解决。
2) 使用 boolean namechanged 而不是 onpropertychaned。每当调用该属性并使用布尔值触发错误时。
3)在停止按钮中将所有属性设置为空,并且我使用 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;
}
}
推荐阅读
- json - 如何在 Github Actions 中添加传递 env 变量作为 json 的一部分?
- matlab - 如何让 MATLAB 求解方程的根?
- teradata - Teradata - 合并更新与条件
- reactjs - 使用字符串数组参数搜索字符串数组
- javascript - 为什么我不能将透视相机配置为三个js中的实际尺寸?
- typescript - 运行 `tsc -p .` 产生“未知编译器选项”错误
- angular - Kendo Grid Angular - 基于 orderIdex 的列自定义顺序
- python - 将数组从单元格移动到列标题和行值
- reactjs - 通过转置网格覆盖material-ui最大列数?
- typescript - Typescript Decorator 函数泛型类型