I am a newbie at .net development so please help me out here.
I am trying to pass a value from c# class to a validation rule by means of xaml data binding.
C# class:
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);
}
}
Here SimpleValueContainerVM<T>
is a generic class that is used to get and set the values from corresponding UI elements.
Xaml code :
<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>
Here ValueWithinLimits Rule is the one am working with :
The validation Rule is given as :
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);
}
}
I tried something like
<validationRule:ValueWithinLimitsRule ValidatesOnTargetUpdated="True" ValidationStep="RawProposedValue" MinVal="0" MaxVal="100"/>
and that just works fine.
Now i want to use the properties of NumericDoubleUpDownValueContainerVM
MinimumValue and MaximumValue
in place of 0 and 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.