在button点击之前存储滑块值

button_click我必须静音或取消静音。 它工作正常,但我想设置滑块值取消静音之前,它是静音,而不是固定的0.7。

 private void btnMute_Click(object sender, RoutedEventArgs e) { if (slider.Value <= 0.0) { slider.Value = 0.7; btnMute.Content = "Mute"; } if (slider.Value > 0.0) { slider.Value = 0.0; btnMute.Content = " UnMute"; } } 

Solutions Collecting From Web of "在button点击之前存储滑块值"

窗口

  <Grid> <Slider HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="250" ValueChanged="Slider_ValueChanged"/> </Grid> 

代码后面:

  public partial class MainWindow : Window { private double _lastSliderValue; public MainWindow() { InitializeComponent(); _lastSliderValue = 0.7; } private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) { // ... Get Slider reference. var slider = sender as Slider; // ... Get Value. if(slider.Value != 0.0) _lastSliderValue = slider.Value; // ... Set Window Title. this.Title = "Value: " + value.ToString("0.0") + "/" + slider.Maximum; } private void btnMute_Click(object sender, RoutedEventArgs e) { if (slider.Value <= 0.0) { slider.Value = _lastSliderValue; btnMute.Content = "Mute"; } else { slider.Value = 0.0; btnMute.Content = " UnMute"; } } }