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 again,
Andrew, I tried your solution and it solves part of my problem. In a situation where I use the control directly in a window it works fine now and the errors are gone, thanks!.
However when I make a usercontrol and set some dependencyproperties to my max/min values the control ends up having no range at all and the only valid value is the max or min value that is last in the xaml.
here is the code from the usercontrol setting the depprops
public static readonly DependencyProperty MinValProperty =
DependencyProperty.Register("ValueMinMine", typeof(double), typeof(UserControl1));
public double ValueMinMine
{
get
return (double)GetValue(UserControl1.MinValProperty);
}
set
SetValue(UserControl1.MinValProperty, value);
public static readonly DependencyProperty MaxValProperty =
DependencyProperty.Register("ValueMaxMine", typeof(double), typeof(UserControl1));
public double ValueMaxMine
return (double)GetValue(UserControl1.MaxValProperty);
SetValue(UserControl1.MaxValProperty, value);
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 };
and the xaml from the usercontrol
<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="1"
ValueType="{x:Type my:Double}"
VerticalAlignment="Top" Width="138">
<igEditors:XamNumericEditor.ValueConstraint>
<MultiBinding Converter="{StaticResource valueConstraintConverter}">
<Binding ElementName="MyNumEdit" Path="ValueMaxMine" Mode="OneTime" diag:PresentationTraceSources.TraceLevel="High" />
<Binding ElementName="MyNumEdit" Path="ValueMinMine" Mode="OneTime" diag:PresentationTraceSources.TraceLevel="High"/>
</MultiBinding>
</igEditors:XamNumericEditor.ValueConstraint>
</igEditors:XamNumericEditor>
Am I missing something here?
Thanks
Hans
Thanks for the fast answer! very impressive :)
hans
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>
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}"
SpinIncrement="0.5"
<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}"
/>
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')