滑块最小/最大绑定到Int常量

时间:2022-06-19 00:27:16

I have an integer constants to define minimum and maximum values of some integer data, and i want to bind them to a Slider control properties like im doing on another numeric editor, but seems impossible.

我有一个整数常量来定义一些整数数据的最小值和最大值,我想将它们绑定到Slider控件属性,就像我在另一个数字编辑器上做的那样,但似乎不可能。

Is there any easy way to acomplish this? Maybe with value converters, or Im missing something?

有没有简单的方法来实现这个?也许有价值转换器,或我错过了什么?

A bit of example code:

一些示例代码:

public const Int32 EXAMPLE_MIN_VALUE = 23;
public const Int32 EXAMPLE_MAX_VALUE = 55;

This works ok, im using an integer editor of WpfToolkit:

这工作正常,我使用WpfToolkit的整数编辑器:

<WpfToolkit:IntegerUpDown Value="{Binding ExampleValue}" 
    Minimum="{x:Static Model:Configuracion.EXAMPLE_MIN_VALUE}" 
    Maximum="{x:Static Model:Configuracion.EXAMPLE_MAX_VALUE}" />

But when I try the same with Slider, it crash:

但是当我尝试使用Slider时,它会崩溃:

<Slider Value="{Binding ExampleValue}" 
    Minimum="{x:Static Model:Configuracion.EXAMPLE_MIN_VALUE}" 
    Maximum="{x:Static Model:Configuracion.EXAMPLE_MAX_VALUE}" />

3 个解决方案

#1


5  

If you use x:Static directly that has no room for type conversion and thus causes an exception as you try to set an int on a double-property, but if you do this it works just fine:

如果直接使用x:Static,它没有进行类型转换的空间,因此当你尝试在double属性上设置int时会导致异常,但如果你这样做,它就可以正常工作:

Minimum="{Binding Source={x:Static local:MainWindow.TestConstInt}}"

That is because bindings apply type converters where necessary. And even if there was no suitable type converter you could just add a Binding.Converter.

这是因为绑定在必要时应用类型转换器。即使没有合适的类型转换器,您也可以添加一个Binding.Converter。

#2


0  

DataBinding doesn't work with fields, only with properties.

DataBinding不适用于字段,仅适用于属性。

Take a look, you have:

看看,你有:

Minimum="{x:Static Model:Configuracion.EXAMPLE_MIN_VALUE}"

In the first example and:

在第一个例子中:

Minimum="{Binding Model:Configuracion.EXAMPLE_MIN_VALUE}"

#3


0  

This is the error message that comes up: {"'23' is not a valid value for property 'Minimum'."}

这是出现的错误消息:{“'23'不是属性'Minimum'的有效值。”}

Change the constant binding to double and it should work!

将常量绑定更改为double,它应该工作!

public const double EXAMPLE_MIN_VALUE = 23.0;
public const double EXAMPLE_MAX_VALUE = 55.0;

#1


5  

If you use x:Static directly that has no room for type conversion and thus causes an exception as you try to set an int on a double-property, but if you do this it works just fine:

如果直接使用x:Static,它没有进行类型转换的空间,因此当你尝试在double属性上设置int时会导致异常,但如果你这样做,它就可以正常工作:

Minimum="{Binding Source={x:Static local:MainWindow.TestConstInt}}"

That is because bindings apply type converters where necessary. And even if there was no suitable type converter you could just add a Binding.Converter.

这是因为绑定在必要时应用类型转换器。即使没有合适的类型转换器,您也可以添加一个Binding.Converter。

#2


0  

DataBinding doesn't work with fields, only with properties.

DataBinding不适用于字段,仅适用于属性。

Take a look, you have:

看看,你有:

Minimum="{x:Static Model:Configuracion.EXAMPLE_MIN_VALUE}"

In the first example and:

在第一个例子中:

Minimum="{Binding Model:Configuracion.EXAMPLE_MIN_VALUE}"

#3


0  

This is the error message that comes up: {"'23' is not a valid value for property 'Minimum'."}

这是出现的错误消息:{“'23'不是属性'Minimum'的有效值。”}

Change the constant binding to double and it should work!

将常量绑定更改为double,它应该工作!

public const double EXAMPLE_MIN_VALUE = 23.0;
public const double EXAMPLE_MAX_VALUE = 55.0;