Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
130
XamSlider Command binding to ThumbValueChanged event
posted

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

Parents
  • 2660
    Offline posted

    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:

    <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

Reply
  • 2660
    Verified Answer
    Offline posted in reply to Anushri Jain

    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 Pachilova
    Associate Software Developer

Children