问题描述
我是.net开发的新手,所以请在这里帮助我.
I am a newbie at .net development so please help me out here.
我正试图通过xaml数据绑定将c#类中的值传递给验证规则.
I am trying to pass a value from c# class to a validation rule by means of xaml data binding.
C#类:
public class NumericDoubleUpDownValueContainerVM : SimpleValueContainerVM<string>
{
public NumericDoubleUpDownValueContainerVM(double value, double minValue, double maxValue, int decimalPlace) : base(value.ToString())
{
this.MinimumValue = minValue;
this.MaximumValue = maxValue;
this.DecimalPlaces = decimalPlace;
}
public double MinimumValue { get; set; }
public double MaximumValue { get; set; }
public int DecimalPlaces { get; set; }
public override void UpdatePropertyValue(object value, string propertyName = "")
{
this.Value = Convert.ToString(value);
}
}
此处 SimpleValueContainerVM< T>
是一个通用类,用于从相应的UI元素获取和设置值.
Here SimpleValueContainerVM<T>
is a generic class that is used to get and set the values from corresponding UI elements.
Xaml代码:
<DataTemplate DataType="{x:Type VM:NumericDoubleUpDownValueContainerVM}" >
<Grid x:Name="Maingrid" >
<WPFStyles.CustomControls:NumericUpDown Minimum="{Binding MinimumValue}" Maximum="{Binding MaximumValue}" x:Name="Value" Width="{Binding ActualWidth, ElementName=Maingrid}"
VerticalAlignment="Center" YIncrementValue="0.1" DecimalPlaces ="{Binding DecimalPlaces}"
ToolTip="{Binding ValueToolTip, Converter={x:Static utils:StringToLocalizedStringConverter.Instance}, ConverterParameter=ToolTip}">
<Binding Path="Value" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay" ValidatesOnDataErrors="True">
<Binding.ValidationRules>
<validationRule:DoubleValidationRule/>
<validationRule:ValueWithinLimitsRule ValidatesOnTargetUpdated="True" ValidationStep="RawProposedValue" />
</Binding.ValidationRules>
</Binding>
</WPFStyles.CustomControls:NumericUpDown>
</Grid>
</DataTemplate>
这里的ValueWithinLimits规则是正在使用的规则:
Here ValueWithinLimits Rule is the one am working with :
验证规则为:
public class ValueWithinLimitsRule : ValidationRule
{
public double MaxVal { get; set; }
public double MinVal { get; set; }
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
if (value != null)
{
if (Convert.ToDouble(value.ToString()) > this.MaxVal || Convert.ToDouble(value.ToString()) < this.MinVal)
{
return new ValidationResult(false, null);
}
else
{
return new ValidationResult(true, null);
}
}
return new ValidationResult(false, null);
}
}
我尝试了
<validationRule:ValueWithinLimitsRule ValidatesOnTargetUpdated="True" ValidationStep="RawProposedValue" MinVal="0" MaxVal="100"/>
那很好用.
现在我要使用 NumericDoubleUpDownValueContainerVM
MinimumValue and MaximumValue
代替0和100.
我尝试使用谷歌搜索并了解依赖属性和对象,但是无法掌握它.
I have tried googling and getting to know dependency properties and objects however cant get a hold of it.
我将非常感谢您的帮助.
I would really appreciate any help.
推荐答案
您可以创建一个包装类,该包装类从 DependencyObject
派生并公开依赖项属性.然后,将CLR属性添加到 ValidationRule
类中,该类返回此包装器类型的实例.
You could create a wrapper class that derives from DependencyObject
and exposes a dependency property. Then you add a CLR property to the ValidationRule
class that returns an instance of this wrapper type.
public class AgeValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
string text = value.ToString();
int age;
int.TryParse(text, out age);
if (age < 10 || age > this.Wrapper.MaxAge)
return new ValidationResult(false, "Invalid age.");
return ValidationResult.ValidResult;
}
public Wrapper Wrapper { get; set; }
}
public class Wrapper : DependencyObject
{
public static readonly DependencyProperty MaxAgeProperty =
DependencyProperty.Register("MaxAge", typeof(int),
typeof(Wrapper), new FrameworkPropertyMetadata(int.MaxValue));
public int MaxAge
{
get { return (int)GetValue(MaxAgeProperty); }
set { SetValue(MaxAgeProperty, value); }
}
}
XAML:
<TextBox xmlns:local="clr-namespace:WpfApplication1">
<TextBox.Text>
<Binding Path="Age" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<local:AgeValidationRule>
<local:AgeValidationRule.Wrapper>
<local:Wrapper MaxAge="{Binding MaxAge}"/>
</local:AgeValidationRule.Wrapper>
</local:AgeValidationRule>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
有关更多信息和完整示例,请参阅以下文章.
Please refer to the following article for more information and a full example.
WPF:将数据绑定值传递给验证规则: https://social.technet.microsoft.com/wiki/contents/articles/31422.wpf-passing-a-data-bound-value-to-a-validation-rule.aspx
这篇关于如何将xaml中的值绑定到验证规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!