Click to See Complete Forum and Search --> : hiding a button on click


neo_xaml
May 7th, 2009, 07:29 PM
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?

gstercken
May 9th, 2009, 03:27 PM
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:

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

Arjay
May 15th, 2009, 03:59 AM
Another approach is the use a custom style for a ToggleButton.

<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:
<Trigger Property="IsChecked" Value="true" >
<Setter Property="Visibility" Value="Hidden" />
</Trigger>

neo_xaml
May 15th, 2009, 02:15 PM
Yes, I ended implementing the following.

<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>

gstercken
May 15th, 2009, 03:14 PM
@Arjay: Cool! :thumb: 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... ;)