I'm trying to bind a decimal value (SalePrice) from my database to the MaxInclusive property of a ValueConstraint like this:
<Label Margin="0,0,4,0" Content="{Binding Path=SalePrice}"/><igEditors:XamNumericEditor x:Name="xnumAmountSaleRefunded"> <igEditors:XamNumericEditor.ValueConstraint> <igEditors:ValueConstraint ValidateAsType="Decimal" MinInclusive="0" MaxInclusive="{Binding Path=SalePrice, Mode=OneWay}" /> </igEditors:XamNumericEditor.ValueConstraint></igEditors:XamNumericEditor>
I added a lable just above the XamNumericEditor just to verify that the binding to SalePrice works, and it does. It shows a value of 24.95 when run.
The SalePrice field in the SQL database is defined as Decimal(10,2).
The problem is that the constraint just doesn't seem to do anything. I can enter any max value and it doesn't catch it.
If I change it to say MaxInclusive="24.95", of course it works. I've tried it with and without the Mode=OneWay, and I've also tried changing the ValidateAsType to Float, Double, Unknown, even Integer64 and a few others and nothing seems to work.
What am I doing wrong?
Hi, I am evaluating the Infragistics WPF controls, I seem to have the same problem:
<igEditors:XamNumericEditor
Value=
"{Binding Path=ValueMine, diag:PresentationTraceSources.TraceLevel=High, ElementName=MyNumEdit, Mode=TwoWay}"
x:Name="MySpinner"
HorizontalAlignment="Left"
Margin="10,42,0,0"
Mask="{}{double:6.2}"
PromptChar=" "
SpinButtonDisplayMode="Always"
SpinIncrement="0.5"
ValueType="{x:Type my:Double}"
VerticalAlignment="Top" Width="138">
<igEditors:XamNumericEditor.ValueConstraint>
<igEditors:ValueConstraint
MaxInclusive=
"{Binding Path=ValueMaxMine, diag:PresentationTraceSources.TraceLevel=High, ElementName=MyNumEdit, Mode=TwoWay }"
MinInclusive=
"{Binding Path=ValueMinMine, diag:PresentationTraceSources.TraceLevel=High, ElementName=MyNumEdit, Mode=TwoWay}"
/>
</igEditors:XamNumericEditor.ValueConstraint>
</igEditors:XamNumericEditor>
Looking at the output window :
...
System.Windows.Data Warning: 65 : BindingExpression (hash=50340407): Resolving source (last chance)
System.Windows.Data Warning: 67 : BindingExpression (hash=50340407): Framework mentor not found
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=ValueMaxMine; DataItem=null; target element is 'ValueConstraint' (HashCode=50690790); target property is 'MaxInclusive' (type 'Object')
System.Windows.Data Warning: 65 : BindingExpression (hash=50070782): Resolving source (last chance)
System.Windows.Data Warning: 67 : BindingExpression (hash=50070782): Framework mentor not found
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=ValueMinMine; DataItem=null; target element is 'ValueConstraint' (HashCode=50690790); target property is 'MinInclusive' (type 'Object')
ElementName bindings would only work for properties of objects in the visual tree (i.e. uielements). The ValueConstraint is a subobject that contains constraint related properties but it is not a visual element so you cannot do element name bindings on its properties. This works for the Value property because that is a property of the visual element (the xamNumericEditor in this case). If you wanted you could probably create your own IValueConverter that would create a ValueConstraint. e.g.
public class ValuesToValueConstraintConverter : IMultiValueConverter { object IMultiValueConverter.Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (values == null || values.Length != 2) return Binding.DoNothing; ValueConstraint vc = new ValueConstraint(); vc.MinInclusive = values[0]; vc.MaxInclusive = values[1]; return vc; } object[] IMultiValueConverter.ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) { return new object[] { Binding.DoNothing }; } }
ValueConstraint vc = new ValueConstraint(); vc.MinInclusive = values[0]; vc.MaxInclusive = values[1];
return vc; }
object[] IMultiValueConverter.ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) { return new object[] { Binding.DoNothing }; } }
Then you would use it like this:
<StackPanel xmlns:local="clr-namespace:WpfApplication3"> <StackPanel.Resources> <local:ValuesToValueConstraintConverter x:Key="valueConstraintConverter" /> </StackPanel.Resources> <igEditors:XamNumericEditor x:Name="numMin" Value="0" SpinButtonDisplayMode="Always" /> <igEditors:XamNumericEditor x:Name="numMax" Value="20" SpinButtonDisplayMode="Always" /> <igEditors:XamNumericEditor> <igEditors:XamNumericEditor.ValueConstraint> <MultiBinding Converter="{StaticResource valueConstraintConverter}"> <Binding ElementName="numMin" Path="Value" /> <Binding ElementName="numMax" Path="Value" /> </MultiBinding> </igEditors:XamNumericEditor.ValueConstraint> </igEditors:XamNumericEditor> </StackPanel>