单击按钮两次以确认操作

Click button twice to confirm action

所以我想找到一种方法,让用户通过单击按钮两次 单击来确认他们的操作。 第一次它会通过一个隐藏的标签显示警告并启动一个计时器,该计时器会计时几秒钟。然后,如果用户再次单击按钮(在超时之前),它将执行代码。 如果离开,标签将隐藏,用户将不得不再次单击 2 次以 运行 代码。 最好的方法是否涉及 if 参数和全局变量。当我在想这样的事情时 (我知道它不在 c# 中,但它是我正在谈论的内容的良好视觉表示):

Var buttonclick = 0
if buttonclick = 0
{Show warning label
Start timer 
Set Var buttonclick = 1}
If buttonclick = 1
{Do action}

或者还有其他更好的方法吗?

我不确定使用计时器是否是个好主意,但我使用三态 ToggleButton 来实现类似的行为。

在我的解决方案中,当用户点击我的按钮(我称之为 ConfirmCancelButton)时,它会变成红色并等待第二次点击,如果发生第二次点击,那么我的 "cancel command" 将执行但是如果按钮失去焦点(例如,用户点击其他地方),它将回到原来的状态。

这是一个简单的例子:

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

<Window.Resources>
    <Style x:Key="ConfirmCancelButton" TargetType="{x:Type ToggleButton}">
        <Setter Property="IsThreeState" Value="True"/>
        <Style.Triggers>
            <!-- Button is not clicked (original state) -->
            <Trigger Property="IsChecked" Value="False">
                <Setter Property="Foreground" Value="{DynamicResource TextBrush}"/>
            </Trigger>
            <!-- Button clicked once (waiting for 2nd click)-->
            <Trigger Property="IsChecked" Value="True">
                <Setter Property="Foreground" Value="Orange"/>
            </Trigger>
            <!-- Button clicked twice (executing cancel command)-->
            <Trigger Property="IsChecked" Value="{x:Null}">
                <Setter Property="Foreground" Value="Red"/>
            </Trigger>
            <!-- Button lost it's focus (back to the original state)-->
            <Trigger Property="IsFocused" Value="False">
                <Setter Property="IsChecked" Value="False"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>


<Window>
    <Grid>
        <ToggleButton Grid.Row="1" Style="{StaticResource ConfirmCancelButton}" Content="Cancel" >
            <i:Interaction.Triggers>
                <!-- if IsChecked=="{x:Null}" -->
                <i:EventTrigger EventName="Indeterminate">
                    <i:InvokeCommandAction Command="{Binding CancelCommand}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </ToggleButton>
    </Grid>
</Window>