Ahmet Melih Başbuğ
Ahmet Melih Başbuğ

Reputation: 189

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?

button1.isEnabled = false; //hide button.

I don't want to hide it and click it again.

Upvotes: 1

Views: 2283

Answers (3)

rahulroy9202
rahulroy9202

Reputation: 2848

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. So, no further clicks will invoke the event handler.

Upvotes: 0

lukerobbers
lukerobbers

Reputation: 64

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

or

UIElement.IsEnabled Property

Gets or sets a value indicating whether this element is enabled in the user interface (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

 <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

Upvotes: 4

Romasz
Romasz

Reputation: 29792

Have you tried to put in Click Event such a line?

myButton.IsHitTestVisible = false;

Upvotes: 3

Related Questions