<Window x:Class="CustomMediaPlayerWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Custom Media Player" Activated="window_Activated" Deactivated="window_Deactivated" Closing="window_Closing"> <DockPanel> <Menu DockPanel.Dock="Top"> <MenuItem Header="_File"> <MenuItem Header="_Exit" Click="exitMenu_Click" /> </MenuItem> </Menu> <StackPanel DockPanel.Dock="Top" Orientation="Horizontal" HorizontalAlignment="Center"> <Button Name="playButton" Click="playButton_Click">Play</Button> <Button Name="clickButton" Click="stopButton_Click">Stop</Button> </StackPanel> <MediaElement Stretch="Fill" Name="mediaElement" LoadedBehavior="Manual" Source="numbers.wmv" /> </DockPanel> </Window> //File:Window.xaml.cs using System; using System.ComponentModel; using System.Windows; public partial class CustomMediaPlayerWindow : Window { public CustomMediaPlayerWindow() { InitializeComponent(); } bool isMediaElementPlaying; void playButton_Click(object sender, RoutedEventArgs e) { this.mediaElement.Play(); this.isMediaElementPlaying = true; } void stopButton_Click(object sender, RoutedEventArgs e) { this.mediaElement.Stop(); this.isMediaElementPlaying = false; } void window_Activated(object sender, EventArgs e) { if( this.isMediaElementPlaying ) this.mediaElement.Play(); } void window_Deactivated(object sender, EventArgs e) { if (this.isMediaElementPlaying) this.mediaElement.Pause(); } void exitMenu_Click(object sender, RoutedEventArgs e) { this.Close(); } void window_Closing(object sender, CancelEventArgs e) { if (this.isMediaElementPlaying) { e.Cancel = true; } } }