简体   繁体   English

最小值和最大值之后绑定DoubleUpDown值

[英]Bind DoubleUpDown Value after Minimum and Maximum

I have a UserControl with a ListBox and a DoubleUpDown . 我有一个带ListBoxDoubleUpDownUserControl 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. 当我在ListBox框中选择项目时,我想设置MinimumMaximumDoubleUpDownValue ,以便用户可以更改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: 问题在于此代码首先设置了Value ,然后设置了MinimumMaximum所以如果我具有以下值:

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

The range for One is 4..8 and the range for Two is 0..2. 该范围之一是4..8和两个范围为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. 如果我先选择一个 ,然后选择两个 ,则它看起来像Two.MyVal = 4 ,因为当从One出发的范围仍处于活动状态时,将插入该值。

How to change the code to first set the Minimum and Maximum and after that set the Value ? 如何更改代码以首先设置“ Minimum和“ Maximum ,然后再设置“ Value

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). 从根本上讲,这意味着在设计自己的控件时,请确保省略完整性检查,直到初始化完成(直到IsInitializedtrueIsInitialized Initialized事件为止)。

Apparently the DoubleUpDown was not designed with your exact problem in mind and enforces Min/Max boundaries before initialization is complete. 显然DoubleUpDown在设计时并未考虑您的确切问题,而是在初始化完成之前强制执行Min / Max边界。

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. 编辑: 此特定控件的公共源代码似乎考虑了IsInitialized因此实际上不应出现您遇到的问题。

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. 尽管我找不到任何文档都未明确说明,但Xaml Parser会按遇到对象的顺序设置对象属性。


Possible Solution: 可能的解决方案:

You should be able to work around this problem by setting the Value property last. 您应该能够通过最后设置Value属性来解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM