I'm developing an app with C# and WPF, I've my own slider custom control. and textboxes on the same window. All properties of my slider are DependencyProperty
.
I use textboxes to change slider's properties. I want to use ValidationRule
on textboxes. I wrote my own ValidationRule (derived from ValidationRule class). I want to pass some parameters to that ValidationRule
. Here is code:
TextBox:
<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 ValidationRule:
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); }
}
}
My guess is, everything is fine but, the problem is, NumberCombineTo
parameter is set to default (0)
even if I change gammaSlider's RightThumbValue property.
I need to update NumberCombineTo
property when RightThumbValue
is changed.