我正在做一个WPF的C#项目。
在我的主窗口中,我有5个(可能更晚)的TextBox,每个框都包含一个值在0到100之间的float。
我已经创建了一个验证规则:
class RangeValidationRule : ValidationRule
{
public float MinValue { get; set; }
public float MaxValue { get; set; }
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
float intValue;
string text = String.Format("Must be between {0} and {1}",
MinValue, MaxValue);
if (!float.TryParse(value.ToString(), out intValue))
return new ValidationResult(false, "Not a float");
if (intValue < MinValue)
return new ValidationResult(false, "To small. " + text);
if (intValue > MaxValue)
return new ValidationResult(false, "To large. " + text);
return ValidationResult.ValidResult;
}
}但我不知道如何简化它的使用。实际上我是这样使用它的:
<TextBox Grid.Row="3" Grid.Column="1"
Style="{StaticResource StyleTextBox}">
<TextBox.Text>
<Binding Path="ValueToBind" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<local:RangeValidationRule MinValue="0" MaxValue="100"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>我如何简化我的代码?我可以为这种类型的TextBox创建样式吗?
<Style x:Key="StyleTextBox0To100" TargetType="TextBox"
BasedOn="{StaticResource StyleTextBox}">
<!-- What should I write ? -->
</Style>发布于 2016-12-21 19:59:58
您正在搜索的解决方案是BindingGroup
之前询问过的question
https://stackoverflow.com/questions/41262192
复制相似问题