I am using XamNumericRangeSlider in my application. I want to do command binding to ThumbValueChanged event using MVVM pattern.
I tried using Microsoft interaction event triggers with Relay Command. I am not able to get it right.
Any kind of help is highly appreciated. Thanks
Hello,
I have been looking into your question and created a small sample that demonstrates a command binding to XamNumericRangeSlider's ThumbValueChanged Event. I have added a XamNumericRangeSlide with two thumbs. The thumb values are bound to members of the View Model. There is also a text block, which shows the difference between the thumb values. In the View Model I defined an ICommand - CalculateRangeSpanCommand, which is initialized as RelayCommand:this.CalculateRangeSpanCommand = new RelayCommand(param => CalculateRangeSpanCommandMethod(param), param => true); The sample command method's purpose is to refresh the text block, displaying the difference, upon changing a thumb value. It is defined as follows:public void CalculateRangeSpanCommandMethod(object parameter) { if(parameter != null) { ObservableCollection<XamSliderThumb<Double>> Thumbs = parameter as ObservableCollection<XamSliderThumb<Double>>; RangeSpan = Math.Abs(Thumbs[1].Value - Thumbs[0].Value); } } The Command is bound to the XamNumericRangeSlider's ThumbValueChanged using the Interaction class. I am also passing a parameter to the command, to be able to retrieve the Thumbs collection in the method:
this.CalculateRangeSpanCommand = new RelayCommand(param => CalculateRangeSpanCommandMethod(param), param => true);
public void CalculateRangeSpanCommandMethod(object parameter) { if(parameter != null) { ObservableCollection<XamSliderThumb<Double>> Thumbs = parameter as ObservableCollection<XamSliderThumb<Double>>; RangeSpan = Math.Abs(Thumbs[1].Value - Thumbs[0].Value); } }
<ig:XamNumericRangeSlider Name="xnrs1"> <i:Interaction.Triggers> <i:EventTrigger EventName="ThumbValueChanged"> <i:InvokeCommandAction Command="{Binding Path=CalculateRangeSpanCommand}" CommandParameter="{Binding Path=Thumbs, RelativeSource={RelativeSource AncestorType={x:Type ig:XamNumericRangeSlider}}}"> </i:InvokeCommandAction> </i:EventTrigger> </i:Interaction.Triggers> ... </ig:XamNumericRangeSlider>
I have added a Relay Command class implementation to the sample, however, you could modify it according to your requirements.
Attached could be found my sample for your reference. Please test it on your side and let me know if you require any further assistance on the matter.
Sincerely, Bozhidara Pachilova Associate Software Developer
5875.XNRSCommandBinding.zip
I want to show lables in In xam numeric range slider. Also want to configure range from the code dynamically.
Is it possible to do it?
Hello Anushri,
I believe you will find this topic in our documentation about Value Tick Marks very helpful on the matter. There you will find example code on how to define a data template for a tick mark, displaying text bound to its value.
Regarding your second question, I am wondering if you had the chance to test the previously suggested sample on your side? As it demonstrates the XamNumericRangeSlider in an MVVM-like scenario, it has two thumbs, bound to two numeric values of the view model. Both values are set in the view model, that is from code. Upon change, the new values are reflected as the binding mode is two-way.
Sincerely,Bozhidara PachilovaAssociate Software Developer
Thanks for the help.
I used the link and I could dispay the tick marks.
I have done data binding of maxValue, MinValue and tick frequency from code behind.
The MaxValue and TickFrequency binding are working well, but MinValue binding is not getting reflected on tick frequency.
Any Suggestions?
I am glad that you have successfully managed to display the tick marks.
If I understand correctly, the issue you are facing is that both the regular and value tick marks are not displayed for the MinValue/MaxValue. Please, correct me if my assumption is wrong.
What I can suggest is setting the IncludeSliderEnds property of the SliderTickMarks objects to “True”:
<ig:XamNumericRangeSlider.TickMarks> <ig:SliderTickMarks UseFrequency="True" NumberOfTickMarks="10" IncludeSliderEnds="True"> </ig:SliderTickMarks> <ig:SliderTickMarks TickMarksFrequency="5" UseFrequency="True" HorizontalTickMarksTemplate="{StaticResource HorizontalTick}" IncludeSliderEnds="True" /> </ig:XamNumericRangeSlider.TickMarks>
I am also attaching the modified sample showing this as well as binding to the XamNumericRangeSlider’s MinValue and MaxValue properties. On my side, everything seems to be working as expected and no binding errors are observed.
Please, test it on your side and if you require any further assistance on the matter, please let me know.
Best regards,Bozhidara Pachilova
3858.XNRSommandBinding.zip