[英]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;
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.