文本框的仅代码隐藏验证规则
Code-behind only validation rule of a textbox
我在 XAML 中有一个针对 TextBox
es 的 Window 资源样式,该样式触发和验证。这些非常适合我的 XAML TextBox
es。到目前为止,还不错。
我想做的不同于 TextBox
es 是动态创建的,而不是 XAML。
请特别注意有一个参数要传递给 ValidationRule
,名为 WhatToCheck。
到目前为止,这是我的代码:
private StackPanel NumberList(string queryLabel, string businessModelObjectName)
{
TestData currentTestData = dTestDataHolder["CurrentTestData"];
StackPanel spNumberList = new StackPanel();
spNumberList.Orientation = Orientation.Horizontal;
spNumberList.FlowDirection = System.Windows.FlowDirection.RightToLeft;
spNumberList.Margin = new Thickness(0, 0, 0, 10);
Label lNumberList = new Label();
lNumberList.Content = queryLabel;
lNumberList.FontSize = 16;
lNumberList.FontWeight = FontWeights.Bold;
spNumberList.Children.Add(lNumberList);
TextBox tbNumberList = new TextBox();
tbNumberList.Margin = new Thickness(10, 0, 0, 0);
tbNumberList.FontSize = 16;
tbNumberList.VerticalContentAlignment = VerticalAlignment.Center;
tbNumberList.FontFamily = new FontFamily("Courier New");
tbNumberList.Width = 100;
tbNumberList.AcceptsReturn = false;
tbNumberList.FlowDirection = FlowDirection.LeftToRight;
spNumberList.Children.Add(tbNumberList);
Binding bindingNumberList = new Binding(businessModelObjectName);
bindingNumberList.Source = currentTestData;
bindingNumberList.Mode = BindingMode.TwoWay;
tbNumberList.SetBinding(TextBox.TextProperty, bindingNumberList);
return spNumberList;
}
public class GenericValidationRule : ValidationRule
{
public string WhatToCheck{ get; set; }
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
var enteredString = value as string;
switch (WhatToCheck)
{
case "No Extension":
{
if (enteredString.IndexOf(".doc", StringComparison.InvariantCultureIgnoreCase) >= 0)
{
return new ValidationResult(false, String.Format("SOMETHING"));
}
break;
}
case "Only Numbers":
{
double dDummy;
if (!double.TryParse(enteredString, out dDummy))
{
return new ValidationResult(false, String.Format("SOMETHING"));
}
break;
}
default:
break;
}
return new ValidationResult(true, null);
}
}
- 使用 c# 和 Visual Studio 2013.
- 没有 MVVM。
谢谢!
我最终做到了这一点。效果很好,
Binding bindingNumberList = new Binding(businessModelObjectName);
bindingNumberList.Source = currentTestData;
bindingNumberList.Mode = BindingMode.TwoWay;
bindingNumberList.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
bindingNumberList.ValidatesOnDataErrors = true;
GenericValidationRule gvrCheckValidImageNubers = new GenericValidationRule();
gvrCheckValidImageNubers.ValidationStep = ValidationStep.RawProposedValue;
gvrCheckValidImageNubers.WhatToCheck = "Contains Existing Image Numbers";
bindingNumberList.ValidationRules.Add(gvrCheckValidImageNubers);
tbNumberList.SetBinding(TextBox.TextProperty, bindingNumberList);
我在 XAML 中有一个针对 TextBox
es 的 Window 资源样式,该样式触发和验证。这些非常适合我的 XAML TextBox
es。到目前为止,还不错。
我想做的不同于 TextBox
es 是动态创建的,而不是 XAML。
请特别注意有一个参数要传递给 ValidationRule
,名为 WhatToCheck。
到目前为止,这是我的代码:
private StackPanel NumberList(string queryLabel, string businessModelObjectName)
{
TestData currentTestData = dTestDataHolder["CurrentTestData"];
StackPanel spNumberList = new StackPanel();
spNumberList.Orientation = Orientation.Horizontal;
spNumberList.FlowDirection = System.Windows.FlowDirection.RightToLeft;
spNumberList.Margin = new Thickness(0, 0, 0, 10);
Label lNumberList = new Label();
lNumberList.Content = queryLabel;
lNumberList.FontSize = 16;
lNumberList.FontWeight = FontWeights.Bold;
spNumberList.Children.Add(lNumberList);
TextBox tbNumberList = new TextBox();
tbNumberList.Margin = new Thickness(10, 0, 0, 0);
tbNumberList.FontSize = 16;
tbNumberList.VerticalContentAlignment = VerticalAlignment.Center;
tbNumberList.FontFamily = new FontFamily("Courier New");
tbNumberList.Width = 100;
tbNumberList.AcceptsReturn = false;
tbNumberList.FlowDirection = FlowDirection.LeftToRight;
spNumberList.Children.Add(tbNumberList);
Binding bindingNumberList = new Binding(businessModelObjectName);
bindingNumberList.Source = currentTestData;
bindingNumberList.Mode = BindingMode.TwoWay;
tbNumberList.SetBinding(TextBox.TextProperty, bindingNumberList);
return spNumberList;
}
public class GenericValidationRule : ValidationRule
{
public string WhatToCheck{ get; set; }
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
var enteredString = value as string;
switch (WhatToCheck)
{
case "No Extension":
{
if (enteredString.IndexOf(".doc", StringComparison.InvariantCultureIgnoreCase) >= 0)
{
return new ValidationResult(false, String.Format("SOMETHING"));
}
break;
}
case "Only Numbers":
{
double dDummy;
if (!double.TryParse(enteredString, out dDummy))
{
return new ValidationResult(false, String.Format("SOMETHING"));
}
break;
}
default:
break;
}
return new ValidationResult(true, null);
}
}
- 使用 c# 和 Visual Studio 2013.
- 没有 MVVM。
谢谢!
我最终做到了这一点。效果很好,
Binding bindingNumberList = new Binding(businessModelObjectName);
bindingNumberList.Source = currentTestData;
bindingNumberList.Mode = BindingMode.TwoWay;
bindingNumberList.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
bindingNumberList.ValidatesOnDataErrors = true;
GenericValidationRule gvrCheckValidImageNubers = new GenericValidationRule();
gvrCheckValidImageNubers.ValidationStep = ValidationStep.RawProposedValue;
gvrCheckValidImageNubers.WhatToCheck = "Contains Existing Image Numbers";
bindingNumberList.ValidationRules.Add(gvrCheckValidImageNubers);
tbNumberList.SetBinding(TextBox.TextProperty, bindingNumberList);