Artholl Artholl - 1 year ago 110
C# Question

Bind DoubleUpDown Value after Minimum and Maximum

I have a

UserControl
with a
ListBox
and a
DoubleUpDown
. When I select the item in the
ListBox
I want to set the
Minimum
,
Maximum
and the
Value
of the
DoubleUpDown
so the user can change the
Value
not from predefined range, but in some range around the given value. I use this code:

<extToolkit:DoubleUpDown Increment="0.1" Value="{Binding SelectedItem.MyValue, ElementName=listBox1, UpdateSourceTrigger=PropertyChanged, Converter={conv:CheckValue}}" FormatString="0.0">
<extToolkit:DoubleUpDown.Maximum>
<MultiBinding Converter="{conv:CorMaximum}">
<Binding Path="SelectedItem.MyValue" ElementName="listBox1" />
<Binding Path="SelectedItem.MaxDif" ElementName="listBox1" />
</MultiBinding>
</extToolkit:DoubleUpDown.Maximum>
<extToolkit:DoubleUpDown.Minimum>
<MultiBinding Converter="{conv:CorMinimum}">
<Binding Path="SelectedItem.MyValue" ElementName="listBox1" />
<Binding Path="SelectedItem.MaxDif" ElementName="listBox1" />
</MultiBinding>
</extToolkit:DoubleUpDown.Minimum>
</extToolkit:DoubleUpDown>


The problem is that this code first set the
Value
and after that set the
Minimum
and
Maximum
so if I have this values:

Name | MyValue | MaxDif
-----------------------
One | 6 | 2
Two | 1 | 1


The range for One is 4..8 and the range for Two is 0..2. If I first select One and then Two it looks like the
Two.MyVal = 4
, because the value is inserted when the range from One is still active.

How to change the code to first set the
Minimum
and
Maximum
and after that set the
Value
?

Answer Source

Official Design Recommendations state

Allow properties to be set in any order even if this results in a temporary invalid state of the object

Which basically means when designing your own control, make sure to omit sanity checks until initialization is complete (until IsInitialized is true and the Initialized event is fired).

Apparently the DoubleUpDown was not designed with your exact problem in mind and enforces Min/Max boundaries before initialization is complete.

Edit: The public source code of this particular control seems to take IsInitialized into account so the problem you are having shouldn't actually occur.

While it is not explicitly stated in any documentation I was able to find, the Xaml Parser sets an objects properties in the order they are encountered.


Possible Solution:

You should be able to work around this problem by setting the Value property last.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download