質問

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.

役に立ちましたか?

解決 2

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;
        }


    }

他のヒント

Lets say that your ViewModel have 2 properties Slider1 and Slider2 and your XAML looks something like this:

<Slider Value="{Binding Path=Slider1}"/>
<Slider Value="{Binding Path=Slider2}"/>

then you can do your logic in ViewModel when Slider1 or Slider2 is changed:

public class MyClass: INotifyPropertyChanged
{
  public event PropertyChangedEventHandler PropertyChanged;

  protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
  {
      var handler = PropertyChanged;
      if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
  }

  private double _slider1;

  public double Slider1
  {
      get { return _slider1; }
      set
      {
          if (_slider1 != value)
          {
              _slider1 = value;
              OnPropertyChanged("Slider1");
              if (_slider1 > Slider2) Slider2 = _slider1;
          }
      }
  }

  private double _slider2;

  public double Slider2
  {
      get { return _slider2; }
      set
      {
          if (_slider2 != value)
          {
              _slider2 = value;
              OnPropertyChanged("Slider2");
              if (_slider2 < Slider1) Slider1 = _slider2;
          }
      }
  }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top