在单击按钮之前存储滑块值

Store slider value before button click

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

Window

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