c# - 基于组合框值,更改刷新计时器

标签 c# wpf xaml

.xaml.cs中我有以下代码

        DispatcherTimer timer = new DispatcherTimer();
        // Default value for auto generation
        timer.Interval = new TimeSpan(0, 0, 5);

        timer.Start();
        timer.Tick += timer_Tick;

.xaml 中,我有以下组合框

        <ComboBox Name="RefreshTick" SelectedIndex="0" SelectionChanged="RefreshTick_Change">
            <ComboBoxItem>1</ComboBoxItem>
            <ComboBoxItem>2</ComboBoxItem>
            <ComboBoxItem>3</ComboBoxItem>
            <ComboBoxItem>4</ComboBoxItem>
            <ComboBoxItem>5</ComboBoxItem>
        </ComboBox>

这意味着每个 ItemValue 将每隔 1、2、3、4 或 5 秒刷新其他地方的一些值。

在我的 RefreshTick_Change 下,我希望在选择 ComboBoxItem 时,刷新时间会发生变化

我尝试了以下方法:

private void RefreshTick_Change(object sender, SelectionChangedEventArgs e)
    {
        if (RefreshTick.SelectedIndex == 0)
        {
            return 1;
        }
        if (RefreshTick.SelectedIndex == 1)
        {
            return 2;
        }
        if (RefreshTick.SelectedIndex == 2)
        {
            return 3;
        }
        if (RefreshTick.SelectedIndex == 3)
        {
            return 4;
        }
        if (RefreshTick.SelectedIndex == 4)
        {
            return 5;
        }
    }

但很明显,return 不是正确的表达方式 谁能帮忙?有没有办法让我不需要五个 if 一个接一个的语句?

最佳答案

试试这个(或非常相似的东西):

private void RefreshTick_Change(object sender, SelectionChangedEventArgs e)
{
    timer.Stop();
    timer.Interval = TimeSpan.FromSeconds(RefreshTick.SelectedIndex + 1);
    timer.Start();
}

关于c# - 基于组合框值,更改刷新计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33881877/

相关文章:

c# - 绑定(bind)到依赖属性,而依赖属性又绑定(bind)到另一个绑定(bind)源

c# - 在 VS 2010 的 Winforms 项目中添加 WPF 窗口

c# - 将 AutoCad 与 WPF C# 应用程序集成

c# - WPF:样式化文本框不显示光标

xaml - 有没有办法在 XAML 中对 TextBlock 进行投影效果?

c# - 多线程多个生产者和使用者线程不会同步BlockingCollection争用条件

c# - [ConfigurationProperty ("providers")] 是做什么的?

c# - 无法在 C# 中创建连接字符串

c# - Microsoft Solver foundation 与 Matlab fmincon 中的约束非线性优化

c# - 如何使用多个参数调用 RelayCommand?