C# 如何将验证绑定到时间选择器(AvalonControlsLibrary)?
我想将自定义验证绑定到TimePicker自定义控件,但下面的代码说“不能向TimePicker的对象类型添加内容”C# 如何将验证绑定到时间选择器(AvalonControlsLibrary)?,c#,.net,wpf,C#,.net,Wpf,我想将自定义验证绑定到TimePicker自定义控件,但下面的代码说“不能向TimePicker的对象类型添加内容” 您应该将绑定放在SelectedTime标签中: <Controls:TimePicker Name="TimePickerEndTime" Grid.Row="2" Grid.Column="1" SelectedHour="11"
您应该将
绑定
放在SelectedTime
标签中:
<Controls:TimePicker Name="TimePickerEndTime"
Grid.Row="2"
Grid.Column="1"
SelectedHour="11"
SelectedMinute="20"
SelectedSecond="0"
>
<Controls:TimePicker.SelectedTime>
<Binding Path="EndTime" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<Validators:MyCustomTimepickerValidation ErrorMessage="{DynamicResource NumberValidatorMesage}"/>
</Binding.ValidationRules>
</Binding>
</Controls:TimePicker.SelectedTime>
</Controls:TimePicker>
2) 创建从验证规则
继承的自定义类:
public class TimeRangeRule : ValidationRule
{
private TimeSpan _min;
private TimeSpan _max;
public TimeRangeRule()
{
}
public TimeSpan Min
{
get { return _min; }
set { _min = value; }
}
public TimeSpan Max
{
get { return _max; }
set { _max = value; }
}
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
if (value != null)
{
TimeSpan time = (TimeSpan)value;
if ((time < Min) || (time > Max))
{
return new ValidationResult(false,
"Please enter the time in the range: " + Min + " - " + Max + ".");
}
else
{
return new ValidationResult(true, null);
}
}
else
return new ValidationResult(true, null);
}
}
公共类时间范围规则:ValidationRule
{
私人时间跨度_min;
私人时间跨度_max;
公共时间范围规则()
{
}
公共时间间隔分钟
{
获取{return\u min;}
设置{u min=value;}
}
公共时间跨度最大值
{
获取{return\u max;}
设置{u max=value;}
}
公共覆盖验证结果验证(对象值,System.Globalization.CultureInfo CultureInfo)
{
if(值!=null)
{
TimeSpan时间=(TimeSpan)值;
如果((时间<最小值)| |(时间>最大值))
{
返回新的ValidationResult(false,
请在范围内输入时间:“+Min+”-“+Max+”;
}
其他的
{
返回新的ValidationResult(true,null);
}
}
其他的
返回新的ValidationResult(true,null);
}
}
3) 使用样式和验证规则编写适当的绑定:
<Controls:TimePicker Name="TimePickerEndTime"
Style="{StaticResource timePickerInError}" >
<Controls:TimePicker.SelectedTime>
<Binding Path="EndTime" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" >
<Binding.ValidationRules>
<Validators:TimeRangeRule Min="10:00:00" Max="15:00:00"/>
</Binding.ValidationRules>
</Binding>
</Controls:TimePicker.SelectedTime>
</Controls:TimePicker>
我输入了,但以这种方式无法使用我的自定义验证器。SelectedTime=“{Binding Path=start\u time,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}”/>我找不到如何内联设置validationrule。@LóriNóda再次检查我的答案。谢谢@kmatyaszek。问题是我将绑定放在了时间选择器标记中,而不是。再次感谢。我不知道为什么我没有看到你的第一个答案。
public class TimeRangeRule : ValidationRule
{
private TimeSpan _min;
private TimeSpan _max;
public TimeRangeRule()
{
}
public TimeSpan Min
{
get { return _min; }
set { _min = value; }
}
public TimeSpan Max
{
get { return _max; }
set { _max = value; }
}
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
if (value != null)
{
TimeSpan time = (TimeSpan)value;
if ((time < Min) || (time > Max))
{
return new ValidationResult(false,
"Please enter the time in the range: " + Min + " - " + Max + ".");
}
else
{
return new ValidationResult(true, null);
}
}
else
return new ValidationResult(true, null);
}
}
<Controls:TimePicker Name="TimePickerEndTime"
Style="{StaticResource timePickerInError}" >
<Controls:TimePicker.SelectedTime>
<Binding Path="EndTime" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" >
<Binding.ValidationRules>
<Validators:TimeRangeRule Min="10:00:00" Max="15:00:00"/>
</Binding.ValidationRules>
</Binding>
</Controls:TimePicker.SelectedTime>
</Controls:TimePicker>