locked
How do I enable/disable buttons at run-time? RRS feed

  • Question

  • How do I create a button that is only available (and visible) after the user has performed a specific action?

    Having come from an ActionScript background, I understand that if I want a button to become available only after another action or period has taken place, I create a key frame on the timeline (or storyboard) at the time when I want the button to appear, and then add the button to that key frame. In this way, the button is not available (nor visible) until that particular key frame has focus.

    Thanks in advance.
    Wednesday, May 19, 2010 12:29 AM

All replies

  • Hi tinbot,

    I guess it wouldn't be the best way to "add" a button at a specific time but to change the visibility.

    In generell use the IsEnabled property to enable/disable the button.

    Here is a short snipped for a storyboard that starts with a button that is visible and enabled, then changes to IsEnabled=False at 0,5 sec., then additionally changes to Visibility=Collapsed at 1,0 sec. and finally ends up in being again visible and enabled at 1,5 sec.

    XAML:

    	<UserControl.Resources>
    		<Storyboard x:Name="sbChangeButton">
    			<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Control.IsEnabled)" Storyboard.TargetName="bt1">
    				<DiscreteObjectKeyFrame KeyTime="0">
    					<DiscreteObjectKeyFrame.Value>
    						<System:Boolean>True</System:Boolean>
    					</DiscreteObjectKeyFrame.Value>
    				</DiscreteObjectKeyFrame>
    				<DiscreteObjectKeyFrame KeyTime="0:0:0.5">
    					<DiscreteObjectKeyFrame.Value>
    						<System:Boolean>False</System:Boolean>
    					</DiscreteObjectKeyFrame.Value>
    				</DiscreteObjectKeyFrame>
    				<DiscreteObjectKeyFrame KeyTime="0:0:1">
    					<DiscreteObjectKeyFrame.Value>
    						<System:Boolean>False</System:Boolean>
    					</DiscreteObjectKeyFrame.Value>
    				</DiscreteObjectKeyFrame>
    				<DiscreteObjectKeyFrame KeyTime="0:0:1.5">
    					<DiscreteObjectKeyFrame.Value>
    						<System:Boolean>True</System:Boolean>
    					</DiscreteObjectKeyFrame.Value>
    				</DiscreteObjectKeyFrame>
    			</ObjectAnimationUsingKeyFrames>
    			<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="bt1">
    				<DiscreteObjectKeyFrame KeyTime="0:0:0.5">
    					<DiscreteObjectKeyFrame.Value>
    						<Visibility>Visible</Visibility>
    					</DiscreteObjectKeyFrame.Value>
    				</DiscreteObjectKeyFrame>
    				<DiscreteObjectKeyFrame KeyTime="0:0:1">
    					<DiscreteObjectKeyFrame.Value>
    						<Visibility>Collapsed</Visibility>
    					</DiscreteObjectKeyFrame.Value>
    				</DiscreteObjectKeyFrame>
    				<DiscreteObjectKeyFrame KeyTime="0:0:1.5">
    					<DiscreteObjectKeyFrame.Value>
    						<Visibility>Visible</Visibility>
    					</DiscreteObjectKeyFrame.Value>
    				</DiscreteObjectKeyFrame>
    			</ObjectAnimationUsingKeyFrames>
    		</Storyboard>
    	</UserControl.Resources>
    
    	<Grid x:Name="LayoutRoot" Background="White">
    		<Button x:Name="bt1" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="123,144,0,0"/>
    	</Grid>

    CODEBEHIND:

     Private Sub bt1_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles bt1.Click
      sbChangeButton.Begin()
     End Sub
    

    Hope this helps.

    Martin

    (Please mark this as answer if this is a solution for you.)

    Wednesday, May 19, 2010 5:27 AM