Search code examples
c#wpfsliders

How to make sliders match values


I'm currently working with sliders in WPF. My GUI window has 2 sliders that are supposed to act together in a few ways. slider1 must always be less than or equal to slider2, and slider2 must always be greater than or equal to slider1. My first attempt at using C# code-behind to solve this problem is documented in my previous question. This question got my code to compile, but did not effect any visual change in my program during run time. What would be the ideal method to making these sliders run in the way that I need them to?

Thank you.


Solution

  • for your ease you ca do this also..

     private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (slider1 == null || slider2 == null)
                return;
            if (slider1.Value >= slider2.Value)
            {
                slider2.Value = slider1.Value;
            }
    
    
    
        }
    
        private void slider2_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (slider1 == null || slider2 == null)
                return;
            if (slider2.Value <= slider1.Value)
            {
                slider1.Value = slider2.Value;
            }
    
    
        }