I know that it is not possible to data bind directly to a ValueContraint because it is an Attached Dependency Property, so I though I might look for a workaround. What I have done is extended the editor and added my own property which does support data binding. Then in the callback for that property, go in and add the ValueContraint that I want set:
private static void OnNumberRspParamChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { NumberRspParam numberRspParam = e.NewValue as NumberRspParam; if (numberRspParam != null) { d.SetValue(ValueConstraint.MaxInclusiveProperty, numberRspParam.MaxValue); d.SetValue(ValueConstraint.MinInclusiveProperty, numberRspParam.MinValue); } }
This doesn't seem to have any effect. I am wondering if there is some way I can make it work. The issue is I am doing a dynamic form system where these parameters are not known until runtime.
Sam
The reason is doesn't work is that you are setting the MaxInclusiveProperty on the control and not on the ValueConstraint of the control. Since you are not changing the value of the properties of the ValueConstraint it will have no effect on the control. So in your property change callback you could get the ValueConstraint for d and then set the Max|MinInclusive property on that.
I understand the concept, but cannot figure out exactly how to get the ValueContraint from the control. So far this is my best guess and it always returns null. Any suggestions?
object valueConstraintProperty = d.GetValue(ValueEditor.ValueConstraintProperty);
All I was talking about was getting the valueconstraint from the editor and then creating one if its null. To take your original snippet:
private static void OnNumberRspParamChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){ NumberRspParam numberRspParam = e.NewValue as NumberRspParam; if (numberRspParam != null) { ValueEditor editor = d as ValueEditor; ValueConstraint constraint = editor.ValueConstraint; if (constraint == null) editor.ValueConstraint = constraint = new ValueConstraint(); constraint.SetValue(ValueConstraint.MaxInclusiveProperty, numberRspParam.MaxValue); constraint.SetValue(ValueConstraint.MinInclusiveProperty, numberRspParam.MinValue); }}
if (numberRspParam != null) { ValueEditor editor = d as ValueEditor; ValueConstraint constraint = editor.ValueConstraint;
if (constraint == null) editor.ValueConstraint = constraint = new ValueConstraint();
constraint.SetValue(ValueConstraint.MaxInclusiveProperty, numberRspParam.MaxValue); constraint.SetValue(ValueConstraint.MinInclusiveProperty, numberRspParam.MinValue); }}
Man, an example would be WONDERFUL. I think I need to do the following, but I don't have a clue to where it should be assigned:
????????? = DependencyProperty.RegisterAttached("ValueConstraint", typeof(ValueConstraint), typeof(UdaNumericEditor));
I have tried adding the property to my class and registering it in my constructor, but when I try to get the value, it doesn't work:
public static readonly DependencyProperty ValueConstraintProperty;
public static readonly DependencyProperty NumberRspParamProperty;
static UdaNumericEditor()
{
ValueConstraintProperty = DependencyProperty.RegisterAttached("ValueConstraint",
typeof(ValueConstraint), typeof(UdaNumericEditor));
NumberRspParamProperty = DependencyProperty.Register("NumberRspParam",
typeof(NumberRspParam), typeof(UdaNumericEditor),
new FrameworkPropertyMetadata(OnNumberRspParamChanged));
//FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata(
// FrameworkPropertyMetadataOptions.None, new PropertyChangedCallback(OnCurrentReadingChanged));
CommandManager.RegisterClassCommandBinding(typeof(UdaNumericEditor),
new CommandBinding(ApplicationCommands.Paste, null));
}
private static void OnNumberRspParamChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
NumberRspParam numberRspParam = e.NewValue as NumberRspParam;
if (numberRspParam != null)
object o = d.GetValue(ValueEditor.ValueConstraintProperty);
ValueConstraint valueConstraint = o as ValueConstraint;
d.SetValue(Infragistics.Windows.Editors.ValueConstraint.MaxInclusiveProperty, numberRspParam.MaxValue);
d.SetValue(Infragistics.Windows.Editors.ValueConstraint.MinInclusiveProperty, numberRspParam.MinValue);
It would be null if you never set the ValueConstraint property in which case maybe you want to create one if it returns null.