简体   繁体   English

单击后禁用按钮

[英]Disable button after click

I want to disable my button after I click it. 我想在单击按钮后禁用它。 I want to see it but I don't want to click again after clicked. 我想看到它,但我不想在单击后再次单击。 İn Windows Phone 8 how can I do this? Windows Phone 8如何执行此操作?

button1.isEnabled = false; //hide button.

I don't want to hide it and click it again. 我不想隐藏它并再次单击它。

UIElement.IsHitTestVisible Property

Gets or sets a value that declares whether this element can possibly be returned as a hit test 获取或设置一个值,该值声明此元素是否可以作为命中测试返回

You can set your_btn.IsHitTestVisible = False at the beginning of your function and then reset it to True at the end 您可以在函数的开头设置your_btn.IsHitTestVisible = False ,然后在结尾处将其重置为True

or 要么

UIElement.IsEnabled Property

Gets or sets a value indicating whether this element is enabled in the user interface (UI) 获取或设置一个值,该值指示是否在用户界面(UI)中启用了此元素

Using IsEnabled (that set the background to transparent), you can create a style resource (going in XAML, right click on the view of your_btn->edit template->edit a copy... , then modifiy the VisualState x:Name="Disabled" field setting the Storyboard.TargetProperty="Background" Value="your_background" . In this way you can also simulate a holding button... The resource style is 使用IsEnabled(将背景设置为透明),可以创建样式资源(在XAML中, right click on the view of your_btn->edit template->edit a copy... ,然后修改VisualState x:Name="Disabled"设置Storyboard.TargetProperty="Background" Value="your_background" VisualState x:Name="Disabled"字段。这样,您还可以模拟一个保持按钮...资源样式为

 <Style x:Key="ButtonStyle1" TargetType="Button">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderBrush" Value="{StaticResource PhoneForegroundBrush}"/>
        <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
        <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/>
        <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilySemiBold}"/>
        <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMedium}"/>
        <Setter Property="Padding" Value="10,5,10,6"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid Background="Transparent">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver"/>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneButtonBasePressedForegroundBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneAccentBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="0" Margin="{StaticResource PhoneTouchTargetOverhang}">
                            <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

And your button will have the Style="{StaticResource ButtonStyle1}" property 并且您的按钮将具有Style="{StaticResource ButtonStyle1}"属性

您是否尝试将Click Event放入这样的行?

myButton.IsHitTestVisible = false;

Unsubscribing from the click event should do the trick 取消点击事件应该可以解决问题

try this - 尝试这个 -

private void button1_Click(object sender, RoutedEventArgs e)
{    
    button1.Click -= button1_Click;

    //Then do what the button does.   
}

So, after the first click, the Event Handler will unsubscribe from the Click Event . 因此,在第一次单击之后, 事件处理程序将取消订阅Click事件 So, no further clicks will invoke the event handler. 因此,不再单击将调用事件处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C#中,如何在第一次单击后禁用一个按钮,然后在单击另一个按钮后再次启用它? - in c#, how can you disable a button after the 1st click, and enable it again after you click another button? 禁用默认按钮单击效果 - Disable default button click effect 单击“后”按钮的方法 - Method for “After” button click 单击后更改按钮的背面颜色,单击两次后将其更改为背面,依此类推 - Change button back color after click, and change it back after 2nd click and so on 单击按钮摆动Java后,事情不显示 - Things dont show after I click my button swing Java 单击按钮[Python] [Selenium]后,切换到打开的新浏览器 - Switch to new browser opened after click on a button [Python][Selenium] 当我单击 WPF (.NET Core) 中第二个窗口的“确定”按钮时,禁用 MainWindow 按钮并返回到 MainWindow 的初始状态 - Disable button of MainWindow and return to the initial state of MainWindow when I click an "OK" button of a Second Window in WPF (.NET Core) 在HtmlHelp中禁用“打印”按钮 - Disable Print button in HtmlHelp powershell无法点击点击按钮 - powershell can not click click a button 将.exe文件放到另一台计算机上时,按一下按钮,程序崩溃 - Program crashes after putting the .exe file on an other computer when pressing a button click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM