感谢您查看此帖子。我只是C#和WPF的初学者,我的小项目正在迅速变成一团糟。我有一个我为调度目的而制作的WPF表单。目前我正在尝试清理一些拼凑在一起的代码。它有点像我想要的方式,但它还没有完全存在。我有一个组合框,文本框,日期和时间选择器的混合,一些控件被隐藏,直到选择否则。我目前所处的是单击按钮控件,它最终会将数据推送到我需要的位置。我想点击按钮初始化的是:
查看所有控件,找到当前可见的控件。在可见控件中,检查它们是否为空/空。如果控件为空/空,请突出显示该字段并显示一条消息,指出“缺少必需数据”。
我在网上浏览了一下,我觉得循环使用这些将是一个非常优越的方法,但是,我普遍缺乏理解使我无法完成这项工作。正如我之前提到的,我有各种控件类型,其中一些来自Xceed WPF工具包。
以下是我提出的建议:
private void Submit_BT_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(PatientName.Text))
{
PatientName.Background = System.Windows.Media.Brushes.LightPink;
System.Windows.Forms.MessageBox.Show("You are missing required data");
}
else
{
PatientName.Background = System.Windows.Media.Brushes.White;
}
if (string.IsNullOrEmpty(MRN.Text))
{
MRN.Background = System.Windows.Media.Brushes.LightPink;
System.Windows.Forms.MessageBox.Show("You are missing required data");
}
else
{
MRN.Background = System.Windows.Media.Brushes.White;
}
if (string.IsNullOrEmpty(Procedure.Text))
{
Procedure.Background = System.Windows.Media.Brushes.LightPink;
System.Windows.Forms.MessageBox.Show("You are missing required data");
}
else
{
Procedure.Background = System.Windows.Media.Brushes.White;
}
if (string.IsNullOrEmpty(Levels.Text))
{
Levels.Background = System.Windows.Media.Brushes.LightPink;
System.Windows.Forms.MessageBox.Show("You are missing required data");
}
else
{
Levels.Background = System.Windows.Media.Brushes.White;
}
if (string.IsNullOrEmpty(Modality_Select.Text))
{
Modality_Select.Background = System.Windows.Media.Brushes.LightPink;
System.Windows.Forms.MessageBox.Show("You are missing required data");
}
else
{
Modality_Select.Background = System.Windows.Media.Brushes.White;
}
if (string.IsNullOrEmpty(Insurance.Text))
{
Insurance.Background = System.Windows.Media.Brushes.LightPink;
System.Windows.Forms.MessageBox.Show("You are missing required data");
}
else
{
Insurance.Background = System.Windows.Media.Brushes.White;
}
if (string.IsNullOrEmpty(Sched.Text))
{
Sched.Background = System.Windows.Media.Brushes.LightPink;
System.Windows.Forms.MessageBox.Show("You are missing required data");
}
else
{
Sched.Background = System.Windows.Media.Brushes.White;
}
if (string.IsNullOrEmpty(OR_Num.Text))
{
OR_Num.Background = System.Windows.Media.Brushes.LightPink;
System.Windows.Forms.MessageBox.Show("You are missing required data");
}
else
{
OR_Num.Background = System.Windows.Media.Brushes.White;
}
if (string.IsNullOrEmpty(Sched_Date.Text))
{
Sched_Date.Background = System.Windows.Media.Brushes.LightPink;
System.Windows.Forms.MessageBox.Show("You are missing required data");
}
else
{
Sched_Date.Background = System.Windows.Media.Brushes.White;
}
if (Start_Time.Value == null)
{
Start_Time.Background = System.Windows.Media.Brushes.LightPink;
System.Windows.Forms.MessageBox.Show("You are missing required data");
}
else
{
Start_Time.Background = System.Windows.Media.Brushes.White;
}
if (string.IsNullOrEmpty(Combobox1.Text))
{
Combobox1.Background = System.Windows.Media.Brushes.LightPink;
System.Windows.Forms.MessageBox.Show("You are missing required data");
}
else
{
Combobox1.Background = System.Windows.Media.Brushes.White;
}
if (End_Time.Value == null)
{
End_Time.Background = System.Windows.Media.Brushes.LightPink;
System.Windows.Forms.MessageBox.Show("You are missing required data");
}
else
{
End_Time.Background = System.Windows.Media.Brushes.White;
}
if (string.IsNullOrEmpty(Combobox2.Text))
{
Combobox2.Background = System.Windows.Media.Brushes.LightPink;
System.Windows.Forms.MessageBox.Show("You are missing required data");
}
else
{
Combobox2.Background = System.Windows.Media.Brushes.White;
}
if (EMG_Type.IsVisible)
{
if (string.IsNullOrEmpty(EMG_Type.Text))
{
EMG_Type.Background = System.Windows.Media.Brushes.LightPink;
System.Windows.Forms.MessageBox.Show("You are missing required data");
}
else
{
EMG_Type.Background = System.Windows.Media.Brushes.White;
}
if (Other_TB.IsVisible)
{
if (string.IsNullOrEmpty(Other_TB.Text))
{
Other_TB.Background = System.Windows.Media.Brushes.LightPink;
System.Windows.Forms.MessageBox.Show("You are missing required data");
}
else
{
Other_TB.Background = System.Windows.Media.Brushes.White;
}
}
}
如何通过循环控件来实现此功能,如果适用,仅显示1条错误消息,但突出显示每个空/空控件?
感谢您提供的任何帮助。
帕特里克
答案 0 :(得分:0)
嗨,我个人不支持这种类型的WPF验证方法,在WPF中我们可以通过使用IDataErrorInfo来实现。
public string Error
{
get
{
return null;
}
}
public string this[string name]
{
ValidateProperty(name);
}
public override void ValidateProperty(string propertyName)
{
Tracer.LogValidation("INotifyDataErrorInfo.ValidateProperty called. Validating " + propertyName);
switch (propertyName)
{
case "x":
{
ValidateNonNegative(x, "x");
ValidateMandatory(x, "x");
}
break;
case "y":
{
ValidateNonNegative(y, "y");
ValidateMandatory(y, "y");
}
break;
}
if (String.IsNullOrEmpty(propertyName))
{
Tracer.LogValidation("No cross-property validation errors.");
}
}