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.
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: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 Value="{Binding ExampleValue}"
Minimum="{x:Static Model:Configuracion.EXAMPLE_MIN_VALUE}"
Maximum="{x:Static Model:Configuracion.EXAMPLE_MAX_VALUE}" />
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:
Minimum="{Binding Source={x:Static local:MainWindow.TestConstInt}}"
That is because bindings apply type converters where necessary. And even if there were no suitable type converter you could just add a Binding.Converter
.
This is the error message that comes up: {"'23' is not a valid value for property 'Minimum'."}
Change the constant binding to double and it should work!
public const double EXAMPLE_MIN_VALUE = 23.0;
public const double EXAMPLE_MAX_VALUE = 55.0;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With