我正在使用 C# 和 WPF 开发一个应用程序,我有自己的滑块自定义控件。和文本框在同一个窗口上。我的滑块的所有属性都是DependencyProperty
.
我使用文本框来更改滑块的属性。我想ValidationRule
在文本框上使用。我编写了自己的 ValidationRule(派生自 ValidationRule 类)。我想向它传递一些参数ValidationRule
。这是代码:
文本框:
<TextBox HorizontalAlignment="Left" Height="24" Margin="10,169,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="40" Style="{DynamicResource archiveSearchTextBox}" MaxLength="3" HorizontalContentAlignment="Center" TabIndex="2">
<TextBox.Text>
<Binding UpdateSourceTrigger="PropertyChanged" Mode="TwoWay" ElementName="gammaSlider" Path="LeftThumbValue" NotifyOnValidationError="True" ValidatesOnExceptions="True" ValidatesOnDataErrors="True">
<Binding.ValidationRules>
<ExceptionValidationRule/>
<local:ZeroTo255MinMax>
<local:ZeroTo255MinMax.Parameters>
<local:ValidationParameters NumberCombineTo="{Binding ElementName=gammaSlider, Path=RightThumbValue}" ValTypeFor0to255="ShouldBeSmaller"/>
</local:ZeroTo255MinMax.Parameters>
</local:ZeroTo255MinMax>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
ZeroTo255MinMax 验证规则:
public class ZeroTo255MinMax : ValidationRule
{
private ValidationParameters _parameters = new ValidationParameters();
public ValidationParameters Parameters
{
get { return _parameters; }
set { _parameters = value; }
}
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
string numberStr = value as string;
int val;
if (int.TryParse(numberStr, out val))
{
if (val < 0 || val > 255)
return new ValidationResult(false, "");
else if (Parameters.ValTypeFor0to255 == ValidationParameters.ValTypes.ShouldBeBigger)
{
if (val <= Parameters.NumberCombineTo || val - Parameters.NumberCombineTo < 2)
return new ValidationResult(false, "");
}
else if (Parameters.ValTypeFor0to255 == ValidationParameters.ValTypes.ShouldBeSmaller)
{
if (val >= Parameters.NumberCombineTo || Parameters.NumberCombineTo - val < 2)
return new ValidationResult(false, "");
}
return new ValidationResult(true, "");
}
else
return new ValidationResult(false, "");
}
}
public class ValidationParameters : DependencyObject
{
public enum ValTypes { ShouldBeSmaller, ShouldBeBigger };
public static readonly DependencyProperty NumberCombineToProperty = DependencyProperty.Register("NumberCombineTo", typeof(int), typeof(ValidationParameters), new PropertyMetadata(0, new PropertyChangedCallback(OnNumberCombineToChanged)));
public static readonly DependencyProperty ValTypeFor0to255Property = DependencyProperty.Register("ValTypeFor0to255", typeof(ValTypes), typeof(ValidationParameters), new PropertyMetadata(ValTypes.ShouldBeBigger));
private static void OnNumberCombineToChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { d.CoerceValue(NumberCombineToProperty); }
public int NumberCombineTo
{
get { return (int)GetValue(NumberCombineToProperty); }
set { SetValue(NumberCombineToProperty, value); }
}
public ValTypes ValTypeFor0to255
{
get { return (ValTypes)GetValue(ValTypeFor0to255Property); }
set { SetValue(ValTypeFor0to255Property, value); }
}
}
我的猜测是,一切都很好,但问题是,即使我更改了 gammaSlider 的 RightThumbValue 属性NumberCombineTo
,参数也设置为。我需要在更改时更新属性。default (0)
NumberCombineTo
RightThumbValue