在.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/