CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2009
    Posts
    12

    hiding a button on click

    I am trying to hide the button once it has been clicked. Each button has its own UID within a datagrid. Is there a way to set or pass a parameter that will hide that particular button?

  2. #2
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815

    Re: hiding a button on click

    You don't need to pass your own parameter for that - in the Click event handler you assign to the buttons, you can evaluate either the 'sender' parameter, or the event arguments' 'Source' property for that:

    Code:
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button source = e.Source as Button;
            if (source != null)
            {
                source.Visibility = Visibility.Hidden;
            }
        }

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: hiding a button on click

    Another approach is the use a custom style for a ToggleButton.

    Code:
    <Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    	<Window.Resources>
    		<Style x:Key="HideButtonStyle" TargetType="{x:Type ToggleButton}">
    			<Setter Property="Foreground" Value="#FFFFFF" />
    			<Setter Property="Width" Value="60"/>
    			<Setter Property="Height" Value="20"/>
    			<Setter Property="FontSize" Value="10"/>
    			<Setter Property="Template">
    				<Setter.Value>
    					<ControlTemplate TargetType="{x:Type ToggleButton}">
    						<Grid>
    							<Rectangle x:Name="Rectangle" Stroke="#FF051F42" StrokeMiterLimit="1.000000" StrokeThickness="0.500000" RadiusX="5" RadiusY="5">
    								<Rectangle.Fill>
    									<LinearGradientBrush EndPoint="0.501,0.039" StartPoint="0.501,0.971">
    										<GradientStop Color="#FF1F317D" Offset="0.101"/>
    										<GradientStop Color="#FF1F317D" Offset="0.49"/>
    										<GradientStop Color="#FF6C8EBD" Offset="0.51"/>
    										<GradientStop Color="#FF283D8C" Offset="0"/>
    										<GradientStop Color="#FF7094C7" Offset="0.986"/>
    									</LinearGradientBrush>
    								</Rectangle.Fill>
    							</Rectangle>
    							<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True"/>
    						</Grid>
    						<ControlTemplate.Triggers>
    							<Trigger Property="IsKeyboardFocused" Value="true"/>
    							<Trigger Property="IsMouseOver" Value="true">
    								<Setter Property="Fill" TargetName="Rectangle">
    									<Setter.Value>
    										<LinearGradientBrush EndPoint="0.501,0.039" StartPoint="0.501,0.971">
    											<GradientStop Color="#FF2C558C" Offset="0.101"/>
    											<GradientStop Color="#FF1F317D" Offset="0.49"/>
    											<GradientStop Color="#FF9DB5D7" Offset="0.51"/>
    											<GradientStop Color="#FF244AAF" Offset="0"/>
    											<GradientStop Color="#FF87A4D9" Offset="0.986"/>
    										</LinearGradientBrush>
    									</Setter.Value>
    								</Setter>
    							</Trigger>
    							<Trigger Property="IsChecked" Value="true" >
    								<Setter Property="Visibility" Value="Hidden" />							
    							</Trigger>
    						</ControlTemplate.Triggers>
    					</ControlTemplate>
    				</Setter.Value>
    			</Setter>
    		</Style>
    	</Window.Resources>
    	<Grid>
    		<ToggleButton Width="80" Height="18" Margin="10,0,0,0" Style="{DynamicResource HideButtonStyle}" >_Hide Me</ToggleButton>
    	</Grid>
    </Window>
    The code that actually hides the button is the following trigger:
    Code:
    <Trigger Property="IsChecked" Value="true" >
    	<Setter Property="Visibility" Value="Hidden" />
    </Trigger>

  4. #4
    Join Date
    Apr 2009
    Posts
    12

    Re: hiding a button on click

    Yes, I ended implementing the following.

    Code:
      <Style TargetType="{x:Type Button}" x:Key="CellTextStyle">
                <Setter Property="IsEnabled" Value="true" />
                <Setter Property="IsTabStop" Value="false" />
                <Style.Triggers>
                    <Trigger Property="Tag" Value="Closed">
                        <Setter Property = "Visibility" Value="Hidden"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
    Last edited by gstercken; May 15th, 2009 at 02:11 PM. Reason: Added code tags - again...

  5. #5
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815

    Re: hiding a button on click

    @Arjay: Cool! As it is good practice with WPF, a declarative / XAML-only approach is always to be considered better than any solution involving code-behind...

    @neo_xaml: Please use code tags when posting code. I'm getting tired of editing posts for that...
    Last edited by gstercken; May 15th, 2009 at 02:20 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured