I insert a Tabcontrol into a BorderControl. The Tabcontrol used seperate styles. Right now the Close Button inside Tabcontrol can refer to itself. So when click it the TabControl is closed I guess so. Is it possible to refer the button to the Border control outside so that when close the Border control is collpased?
<DockPanel LastChildFill="True">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="160" MinWidth="100"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border Margin="5,5,0,5" BorderBrush="Aqua" BorderThickness="1">
<TabControl Margin="0,15,0,0" Style="{StaticResource StandardTabControl}" >
<TabItem Header="Start">
</TabItem>
</TabControl>
</Border>
<GridSplitter Grid.Column="1" Background="Transparent" Width="6" HorizontalAlignment="Center"></GridSplitter>
<Border Grid.Column="2" BorderBrush="Aqua" BorderThickness="1" Margin="0,5,5,5"></Border>
</Grid>
</DockPanel>
After click the close button
And part of the styles of TabControl.
<Button Grid.Column="1" Height="15" Width="15" HorizontalAlignment="Center" VerticalAlignment="Center">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<Ac:CloseTabItemAction TabItem="{Binding RelativeSource={RelativeSource AncestorType=TabItem}}"
TabControl="{Binding RelativeSource={RelativeSource AncestorType=TabControl}}">
</Ac:CloseTabItemAction>
</i:EventTrigger>
</i:Interaction.Triggers>
<!--A lot more about styles-->
</Button>
here is a solution using control template triggers in pure xaml
<DockPanel LastChildFill="True">
<Control>
<Control.Template>
<ControlTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="160"
MinWidth="100"
x:Name="column" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Border Margin="5,5,0,5"
BorderBrush="Aqua"
BorderThickness="1">
<TabControl Margin="0,15,0,0"
Style="{StaticResource StandardTabControl}"
x:Name="tabControl">
<TabItem Header="Start">
</TabItem>
</TabControl>
</Border>
<GridSplitter Grid.Column="1"
Background="Transparent"
Width="6"
HorizontalAlignment="Center"></GridSplitter>
<Border Grid.Column="2"
BorderBrush="Aqua"
BorderThickness="1"
Margin="0,5,5,5"></Border>
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding HasItems,ElementName=tabControl}"
Value="false">
<Setter TargetName="column"
Property="MinWidth"
Value="0" />
<Setter TargetName="column"
Property="Width"
Value="0" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Control.Template>
</Control>
</DockPanel>
based on the assumption that when CloseTabItemAction will be called it will remove the TabItem from the TabControl, the trigger will detect the same based on HasItems property and will collapse the column if there are no items
EDIT
based on the comments here are the finding. while resizing GridSplitter sets a Local value to the ColumnDefinition's Width property which have higher precedence than the Trigger values so the Trigger effectively fails to modify the value.
the solution proposed is to use Animation to set the desired value as Animations has higher precedence then the Local value, and the desired value is applied to the property.
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding HasItems,ElementName=T1}"
Value="false">
<Setter TargetName="Co0"
Property="MinWidth"
Value="0" />
<Setter TargetName="G1"
Property="Width"
Value="0" />
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="Co0"
Storyboard.TargetProperty="Width">
<DiscreteObjectKeyFrame KeyTime="0:0:0">
<DiscreteObjectKeyFrame.Value>
<GridLength>0</GridLength>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</ControlTemplate.Triggers>
in the above code I also make the splitter width to 0 so that there is no empty space on the left
Related
I am trying to change the color of the BorderBrush when the mouse is hovering over the button. I have created a new control template for the button however when setting up the triggers visual studio tells me i have got to use an EventTrigger but when i use this there is no MouseOver event, only a MouseEnter event. When applying this and running the solution the border brush does not change. Any Suggestions?
EDIT:
Code I am having the issue with:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.Resources>
<ControlTemplate x:Key="LogInButton">
<Grid Width="AUTO" Height="AUTO">
<Border x:Name="ButtonBorder"
BorderBrush="#B7B7B7"
BorderThickness="2"
Background="Transparent"
Cursor="Hand">
<Image Source="{Binding Tag, RelativeSource={RelativeSource TemplatedParent}}"/>
<Border.Triggers>
<EventTrigger RoutedEvent="MouseMove">
<EventTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="ButtonBorder"
Storyboard.TargetProperty="BorderBrush"
To="White"
Duration="0"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.EnterActions>
</EventTrigger>
</Border.Triggers>
</Border>
</Grid>
</ControlTemplate>
</Grid.Resources>
<Button Template="{StaticResource LogInButton}" Tag="Images/Login.png" Height="50" Width="50" Background="Transparent">
</Button>
</Grid>
You could just add a Trigger to <ControlTemplate.Triggers> instead of adding an EventTrigger to <Border.Triggers>:
<ControlTemplate x:Key="LogInButton">
<Grid Width="AUTO" Height="AUTO">
<Border x:Name="ButtonBorder"
BorderBrush="#B7B7B7"
BorderThickness="2"
Background="Transparent"
Cursor="Hand">
<Image Source="{Binding Tag, RelativeSource={RelativeSource TemplatedParent}}"/>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="ButtonBorder" Property="BorderBrush" Value="White" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
I have Grid Panel and I want to its border color to change when we click on button
<Grid Grid.Row="3" Name="LocationLayoutPanel" VerticalAlignment="Top"
Margin="0,51,0,0" Height="65" ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="9*" />
<ColumnDefinition Width="2*" />
</Grid.ColumnDefinitions >
<Border BorderThickness="2,2,2,2" BorderBrush="Red" Grid.ColumnSpan="3"
Grid.RowSpan="2"/>
<Button Content="change border color" Grid.Row="1" Grid.Column="1"
Click="chnageBGCOLOR"></Button>
</Grid>
When I click on "change border color" button its grid border color should be changed!
Thank you!
XAML:
<Border BorderThickness="2,2,2,2" Grid.ColumnSpan="3"
Grid.RowSpan="2">
<Border.Style>
<Style TargetType="Border">
<Setter Property="BorderBrush" Value="Red"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=Button, Path=IsPressed}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)"
To="GreenYellow"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>
<Button Name="Button" Content="change boarder color" Grid.Row="1" Grid.Column="1"></Button>
Programically:
Add Name to Border and set as follows:
BorderName.BorderBrush = new SolidColorBrush(Colors.GreenYellow);
I want to make a button like this:
The first image is a normal button, but the second is a button with mouseEnter event fired. The bottom part of the button in the second image is not clickable and it acts like a tool tip. How can I make this kind of a button? Do I need to use a tool tip? Please give me some code examples.
EDIT:
Here's F Ruffel's code which I modified:
<HeaderedContentControl BorderBrush="Black">
<HeaderedContentControl.Header>
<TextBlock Text="Header Placeholder" Margin="5" />
</HeaderedContentControl.Header>
<StackPanel>
<TextBlock Text="Content Placeholder Line 1" Margin="5" HorizontalAlignment="Center"/>
<TextBlock Text="Content Placeholder Line 2" Margin="5" HorizontalAlignment="Center"/>
<TextBlock Text="Content Placeholder Line 3" Margin="5" HorizontalAlignment="Center"/>
</StackPanel>
<HeaderedContentControl.Style>
<Style TargetType="{x:Type HeaderedContentControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type HeaderedContentControl}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Border x:Name="headerBorder" Grid.Row="0" Grid.Column="0" BorderThickness="1" BorderBrush="{TemplateBinding BorderBrush}">
<Grid Name="Header">
<Rectangle Fill="White"/>
<ContentPresenter x:Name="contentPresenter" Content="{TemplateBinding Header}"/>
</Grid>
</Border>
<Border x:Name="contentBorder" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" BorderThickness="1" BorderBrush="{TemplateBinding BorderBrush}" Visibility="Collapsed">
<ContentPresenter />
</Border>
<Border x:Name="cutOutBorder" Grid.Row="0" Grid.Column="1" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="0,0,0,1" Visibility="Collapsed" />
</Grid>
<ControlTemplate.Triggers>
<Trigger SourceName="Header" Property="IsMouseOver" Value="True">
<Setter TargetName="headerBorder" Property="BorderThickness" Value="1,1,1,0" />
<Setter TargetName="contentBorder" Property="BorderThickness" Value="1,0,1,1" />
<Setter TargetName="contentBorder" Property="Visibility" Value="Visible" />
<Setter TargetName="cutOutBorder" Property="Visibility" Value="Visible" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</HeaderedContentControl.Style>
</HeaderedContentControl>
</StackPanel>
Now the last problem is: the expanded part pushes all controls down, and that's what I don't want. Please modify this code to make the extended part overlay controls that are below it, just like in the image.
What I like about WPF is that there is a hundred and one ways to do the same thing. In addition to the suggestions above, you could easily style a HeaderedContentControl to give the desired look and behaviour:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<HeaderedContentControl BorderBrush="Black">
<HeaderedContentControl.Header>
<TextBlock Text="Header Placeholder" Margin="5" />
</HeaderedContentControl.Header>
<StackPanel>
<TextBlock Text="Content Placeholder Line 1" Margin="5" HorizontalAlignment="Center"/>
<TextBlock Text="Content Placeholder Line 2" Margin="5" HorizontalAlignment="Center"/>
<TextBlock Text="Content Placeholder Line 3" Margin="5" HorizontalAlignment="Center"/>
</StackPanel>
<HeaderedContentControl.Style>
<Style TargetType="{x:Type HeaderedContentControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type HeaderedContentControl}">
<Grid x:Name="outerGrid" Background="Transparent">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Border x:Name="headerBorder" Grid.Row="0" Grid.Column="0" BorderThickness="1" BorderBrush="{TemplateBinding BorderBrush}">
<ContentPresenter x:Name="contentPresenter" Content="{TemplateBinding Header}"/>
</Border>
<Border x:Name="contentBorder" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" BorderThickness="1" BorderBrush="{TemplateBinding BorderBrush}" Visibility="Collapsed">
<ContentPresenter />
</Border>
<Border x:Name="cutOutBorder" Grid.Row="0" Grid.Column="1" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="0,0,0,1" Visibility="Collapsed" Background="Transparent" />
</Grid>
</Grid>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="UIElement.MouseLeftButtonUp" SourceName="contentPresenter">
<BeginStoryboard x:Name="makeVisibleBeginStoryboard">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="headerBorder" Storyboard.TargetProperty="BorderThickness">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Thickness Left="1" Top="1" Right="1" Bottom="0" />
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="contentBorder" Storyboard.TargetProperty="BorderThickness">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Thickness Left="1" Top="0" Right="1" Bottom="1" />
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="contentBorder" Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="cutOutBorder" Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="UIElement.MouseLeave" SourceName="outerGrid">
<StopStoryboard BeginStoryboardName="makeVisibleBeginStoryboard" />
</EventTrigger>
<EventTrigger RoutedEvent="UIElement.MouseEnter" SourceName="cutOutBorder">
<StopStoryboard BeginStoryboardName="makeVisibleBeginStoryboard" />
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</HeaderedContentControl.Style>
</HeaderedContentControl>
</StackPanel>
</Window>
This solution simulates a cut-out portion (which is just a cell in a Grid). It uses an EventTrigger so that content is visible when the Header is clicked, and content is made invisible when the mouse leaves the Grid hosting the content, or if it enters the cut-out portion.
Although I think this achieves what you are after, I would recommend investigating other approaches like ToolTip, Adorner, or a even custom control. Using animations like this always feels like over-kill to me :)
I would suggest you to add an adorner to your button and make it visible on Mouse Enter event and just remove the adorner on Mouse Leave event. In case you are new to Adorner's you can look at the brief overview here and few custom samples here-
http://msdn.microsoft.com/en-us/library/ms743737.aspx
http://denisvuyka.wordpress.com/2007/10/15/wpf-simple-adorner-usage-with-drag-and-resize-operations/
This could be an Expander styled appropriately. Here is an image how it normally looks like, but with templates and styles you can drastically alter the appearance of controls. The behaviour looks like an expander that just expands on hover instead of click, though.
I've got a small example that I build myself once. In CSS set #loginBox to display:none. Then when a.loginButton is clicked, let javascript (e.g. jQuery) change display to display:block
<div id="head-login">
<span>Klanten login</span>
<div class="clear"></div>
<div id="loginBox">
<form id="loginForm" action="http://www.dennishunink.nl/dqr/app/login-check.php" method="post">
<fieldset id="body">
<fieldset>
<label for="email">Email Adres</label>
<input type="text" name="email" id="email" />
</fieldset>
<fieldset>
<label for="password">Wachtwoord</label>
<input type="password" name="password" id="password" />
</fieldset>
<input type="submit" id="login" value="Log in" />
<label for="checkbox"><input type="checkbox" id="checkbox" />Onthoudt mij</label>
</fieldset>
<span>Wachtwoord vergeten?</span>
</form>
</div>
</div>
</div>
I've got a data template for a list box item similar to the one on this page...
link
I would like to take it a step further and do something to highlight the items when they change. For example, using the code in the link above, I would like to put a trigger to do something when Widget.Quantity changes. Maybe make the quiantity item (nothing else) flash or something. How can I do that? I include the relevant code below...
<Window.Resources>
<Style x:Key="RoundedItem" TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border CornerRadius="10" BorderBrush="Black" BorderThickness="1" Margin="1">
<ContentPresenter />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<DataTemplate DataType="{x:Type local:Widget}">
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Name}" />
<Label Content="{Binding Quantity}" />
</StackPanel>
</DataTemplate>
</Window.Resources>
<ListBox ItemsSource="{Binding Widgets}" ItemContainerStyle="{StaticResource RoundedItem}" HorizontalContentAlignment="Stretch" />
Just add triggers to the DataTemplate.Triggers collection.
<DataTemplate DataType="{x:Type local:Widget}">
<StackPanel x:Name="panel" Orientation="Horizontal">
<Label Content="{Binding Name}" />
<Label Content="{Binding Quantity}" />
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding SomeProperty}" Value="True">
<Setter Property="Background" Value="Yellow" TargetName="panel" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
You'll probably want to add a property in your Widget class to do that. Or if Widget is a Model, you may want to wrap it in a WidgetViewModel class with an "IsFlashing" property on it. Then set the trigger to fire whenever this "IsFlashing" property is True.
I've managed to get it working with EventTriggers. Complete code below. In summary, the event trigger detects when the bound value changes, then turns the text orange briefly.
<UserControl.Resources>
<!-- instantiate an instance of the TimeSettingsCollection class -->
<c:TimeSettingsCollection x:Key="TimeSettingsCollection"/>
<Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="ItemBorder" BorderBrush="Gray" BorderThickness="1" Margin="3" Padding="7" Background="Transparent">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="ItemBorder" Property="Background" Value="LightBlue" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<DataTemplate DataType="{x:Type c:TimeSettingsItem}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Margin="0,0,8,0"
Style="{StaticResource smallTitleStyle}">Pc Name:</TextBlock>
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=PcName}"
Style="{StaticResource textStyleTextBlock}"/>
<TextBlock Grid.Row="1" Grid.Column="0" Margin="0,0,8,0"
Style="{StaticResource smallTitleStyle}">Time:</TextBlock>
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=TimeSettings.DateTime, NotifyOnTargetUpdated=True}"
Style="{StaticResource textStyleTextBlock}" x:Name="timeTextBlock">
<TextBlock.Background>
<SolidColorBrush Color="Transparent"/>
</TextBlock.Background>
<TextBlock.Triggers>
<EventTrigger RoutedEvent="Binding.TargetUpdated">
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetName="timeTextBlock"
Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"
To="Orange"
Duration="0:0:1"
AutoReverse="True"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBlock.Triggers>
</TextBlock>
</Grid>
</DataTemplate>
</UserControl.Resources>
<DockPanel>
<ListBox Name="timeListBox" ItemsSource="{Binding Source={StaticResource TimeSettingsCollection}}" ItemContainerStyle="{StaticResource ListBoxItemStyle}" HorizontalContentAlignment="Stretch" IsSynchronizedWithCurrentItem="True">
</ListBox>
</DockPanel>
I have a simple style for label controls. I'd like to define a control template inside the style with a button, which could be clicked and would set the visibility property of the label to 'Hidden'. Something like this:
<Style x:Key="MessageLabel_WithCloseButton" TargetType="{x:Type Label}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Label}">
<Border BorderThickness="1" Padding="4" CornerRadius="3"
BorderBrush="Gray" Background="#FFA11616">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ContentPresenter Grid.Column="0"/>
<Button Grid.Column="1" Width="16" Height="16" Padding="2" FontSize="9" Content="X">
<!-- THIS IS WRONG! HOW TO CREATE A TRIGGER FOR THIS BUTTON
HERE AND HOW TO REFER TO THE LABEL? -->
<Button.Triggers>
<Trigger Property="Button.IsPressed" Value="True">
<Setter Property="Visibility" Value="Hidden" />
</Trigger>
</Button.Triggers>
</Button>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The problem is I'm not sure how to handle the click with a trigger and also how to set a property of the label that contains the button.
Thanks.
I authored this with Blend 4. Essentially you want to handle the "PreviewMouseButtonUp" event on your button with an EventTrigger. The EventTrigger will start a Storyboard which animates the UIElement.Visibility property to "Hidden" at the top of the Visual Tree for your label's style.
To get control over the content in the button, you can use the Tag Property on the label control. Otherwise, you will have to create another dependency property, and that means subclassing Label.
Inside the style, then, the <Button/> looks like this:
<Button x:Name="button" Grid.Column="1" Padding="2"
FontSize="9"
Content="{Binding Path=Tag, RelativeSource={RelativeSource TemplatedParent}}"/>
...and since you can put anything into a Tag property you can do this:
<Label x:Name="label" Content="Label"
Style="{DynamicResource MessageLabel_WithCloseButton}">
<Label.Tag>
<StackPanel>
<TextBlock>WOOT</TextBlock>
<TextBlock>WOOT</TextBlock>
</StackPanel>
</Label.Tag>
</Label>
Here is a modified complete style (I also modified some things for better automatic sizing:
<Style x:Key="MessageLabel_WithCloseButton" TargetType="{x:Type Label}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Label}">
<ControlTemplate.Resources>
<Storyboard x:Key="OnClick1">
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="border">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Hidden</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</ControlTemplate.Resources>
<Border x:Name="border" BorderThickness="1" Padding="4" CornerRadius="3"
BorderBrush="Gray" Background="#FFA11616">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ContentPresenter Grid.Column="0" VerticalAlignment="Center" Margin="0,0,3,0"/>
<Button x:Name="button" Grid.Column="1" Padding="2" FontSize="9" Content="{Binding Path=Tag, RelativeSource={RelativeSource TemplatedParent}}"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="UIElement.PreviewMouseLeftButtonUp" SourceName="button">
<BeginStoryboard x:Name="OnClick1_BeginStoryboard" Storyboard="{StaticResource OnClick1}"/>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Note, too, that the EventTrigger is on your ControlTemplate and not on the Button in your tree. But that might be just the way Blend generates the code.
You can use a ToggleButton instead of the normal Button and then just use the IsChecked property for the Trigger:
<Style x:Key="MessageLabel_WithCloseButton" TargetType="{x:Type Label}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Label}">
<Border BorderThickness="1" Padding="4" CornerRadius="3"
BorderBrush="Gray" Background="#FFA11616">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ContentPresenter Grid.Column="0"/>
<ToggleButton x:Name="CloseButton" Grid.Column="1" Width="16" Height="16" Padding="2" FontSize="9" Content="X"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger SourceName="CloseButton" Property="IsChecked" Value="True">
<Setter Property="Visibility" Value="Hidden" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>