<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="ActivationSample.App"
StartupUri="MainWindow.xaml"
Startup="App_Startup"
Activated="App_Activated"
Deactivated="App_Deactivated"
Exit="App_Exit">
</Application>
//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Threading;
namespace ActivationSample
{
public partial class App : System.Windows.Application
{
void App_Startup(object sender, StartupEventArgs e)
{
}
void App_Activated(object sender, EventArgs e)
{
Console.WriteLine("App_Activated");
}
void App_Deactivated(object sender, EventArgs e)
{
Console.WriteLine("Deactivated");
}
void App_Exit(object sender, ExitEventArgs e)
{
Console.WriteLine("Exit");
}
}
}
Event sender, event source and event original source
<Window x:Class="RoutedEvents.BubbledLabelClick"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="BubbledLabelClick" Height="360" Width="330" MouseUp="SomethingClicked" >
<StackPanel MouseUp="SomethingClicked">
<Label Margin="5" BorderThickness="1" MouseUp="SomethingClicked" HorizontalAlignment="Left" >
<StackPanel MouseUp="SomethingClicked" >
<TextBlock Margin="3" MouseUp="SomethingClicked">label</TextBlock>
<Image Source="c:image.jpg" Stretch="None" MouseUp="SomethingClicked" />
<TextBlock Margin="3" MouseUp="SomethingClicked">test</TextBlock>
</StackPanel>
</Label>
<ListBox Margin="5" Name="lstMessages" Grid.Row="1"></ListBox>
<CheckBox Margin="5" Name="chkHandle">Handle first event</CheckBox>
<Button Click="cmdClear_Click" HorizontalAlignment="Right" Margin="5" Padding="3">Clear List</Button>
</StackPanel>
</Window>
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace RoutedEvents
{
public partial class BubbledLabelClick : System.Windows.Window
{
public BubbledLabelClick()
{
InitializeComponent();
}
protected int eventCounter = 0;
private void SomethingClicked(object sender, RoutedEventArgs e)
{
eventCounter++;
string message = "#" + eventCounter.ToString() + ":
" +
" Sender: " + sender.ToString() + "
" +
" Source: " + e.Source + "
" +
" Original Source: " + e.OriginalSource;
lstMessages.Items.Add(message);
e.Handled = (bool)chkHandle.IsChecked;
}
private void cmdClear_Click(object sender, RoutedEventArgs e)
{
eventCounter = 0;
lstMessages.Items.Clear();
}
}
}
Use EventSetter to add mouse event handler
<Window x:Class="Styles.EventSetter"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="EventSetter" Height="300" Width="300">
<Window.Resources>
<Style x:Key="MouseOverHighlightStyle">
<Setter Property="TextBlock.Padding" Value="5"/>
<EventSetter Event="FrameworkElement.MouseEnter" Handler="element_MouseEnter" />
<EventSetter Event="FrameworkElement.MouseLeave" Handler="element_MouseLeave" />
</Style>
</Window.Resources>
<StackPanel>
<TextBlock Style="{StaticResource MouseOverHighlightStyle}">Hover over me.</TextBlock>
<TextBlock Padding="5">asdf</TextBlock>
<TextBlock Style="{StaticResource MouseOverHighlightStyle}">Hover over me.</TextBlock>
</StackPanel>
</Window>
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace Styles
{
public partial class EventSetter : System.Windows.Window
{
public EventSetter()
{
InitializeComponent();
}
private void element_MouseEnter(object sender, MouseEventArgs e)
{
((TextBlock)sender).Background = new SolidColorBrush(Colors.LightGoldenrodYellow);
}
private void element_MouseLeave(object sender, MouseEventArgs e)
{
((TextBlock)sender).Background = null;
}
}
}
Use Triggers to Play Audio When a User Interacts with a Control
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPF" Height="100" Width="300">
<StackPanel>
<MediaElement Name="meMediaElem" />
<UniformGrid Height="70" Columns="2">
<Button Content="Ding" MaxHeight="25" MaxWidth="70">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<MediaTimeline
Source="ding.wav"
Storyboard.TargetName="meMediaElem"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Button.Triggers>
</Button>
<Slider MaxHeight="25" MaxWidth="100" >
<Slider.Triggers>
<EventTrigger RoutedEvent="Slider.ValueChanged">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<MediaTimeline
Source="a.wav"
Storyboard.TargetName="meMediaElem" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Slider.Triggers>
</Slider>
</UniformGrid>
</StackPanel>
</Window>
Style With MultiTrigger
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Window.Resources>
<Style x:Key ="TextBoxStyle" TargetType = "{x:Type TextBox}">
<Setter Property = "Foreground" Value = "Black"/>
<Setter Property = "Background" Value = "LightGray"/>
<Setter Property = "Height" Value = "30"/>
<Setter Property = "Width" Value = "100"/>
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property = "IsFocused" Value = "True"/>
<Condition Property = "IsMouseOver" Value = "True"/>
</MultiTrigger.Conditions>
<Setter Property = "Background" Value = "Yellow"/>
</MultiTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel >
<TextBox Name = "txtOne" Style = "{StaticResource TextBoxStyle}" />
<TextBox Name = "txtTwo" Style = "{StaticResource TextBoxStyle}" />
<TextBox Name = "txtThree" Style = "{StaticResource TextBoxStyle}" />
</StackPanel>
</Window>
Starting an animation with a trigger
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
HorizontalAlignment="Center" VerticalAlignment="Center">
<Ellipse Name="myEllipse" Fill="Red" Height="100" Width="10">
<Ellipse.Triggers>
<EventTrigger RoutedEvent="Ellipse.MouseEnter">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="(Ellipse.Width)"
To="300" Duration="0:0:5" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Ellipse.Triggers>
</Ellipse>
</Page>
Pausing and resuming a storyboard with triggers
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
HorizontalAlignment="Center" VerticalAlignment="Center">
<Ellipse Name="myEllipse" Fill="Red" Height="100" Width="10">
<Ellipse.Triggers>
<EventTrigger RoutedEvent="Ellipse.MouseEnter">
<BeginStoryboard Name="changeWidth">
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="(Ellipse.Width)"
To="300" Duration="0:0:5" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Ellipse.Triggers>
</Ellipse>
</Page>

