1
如何在第一次或最後一次點擊RepeatButton
時指定操作?對repeatButton的第一次或最後一次點擊採取行動
實施例:
標籤最初被設置爲當RepeatButton
按下0
,值被連續
當RepeatButton
是左遞增,該值重置爲0
替代地,當按下按鈕時立即將計數器設置爲0,並開始遞增
如何在第一次或最後一次點擊RepeatButton
時指定操作?對repeatButton的第一次或最後一次點擊採取行動
實施例:
標籤最初被設置爲當RepeatButton
按下0
,值被連續
當RepeatButton
是左遞增,該值重置爲0
替代地,當按下按鈕時立即將計數器設置爲0,並開始遞增
你不需要使用RepeatButton
來做你想做的事。相反,使用標準Button
並處理PreviewMouseDown
和PreviewMouseUp
事件會更好。嘗試是這樣的:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Button Content="Press and Hold" PreviewMouseDown="Button_PreviewMouseDown"
PreviewMouseUp="Button_PreviewMouseUp" />
<TextBlock Grid.Column="1" Text="{Binding YourValue}" />
</Grid>
...
private DispatcherTimer timer = new DispatcherTimer();
private void Button_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
timer.Interval = TimeSpan.FromMilliseconds(100);
timer.Tick += Timer_Tick;
timer.Start();
}
private void Timer_Tick(object sender, EventArgs e)
{
YourValue++;
}
private void Button_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
timer.Stop();
}
可以調整值增量之間的毫秒數,以滿足您的要求。