按钮单击前存储滑块值

时间:2015-04-28 10:16:58

标签: c# windows microsoft-metro

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

1 个答案:

答案 0 :(得分:1)

窗口

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