// PlayButton.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.Navigation; using System.Windows.Shapes; namespace CustomWPF { /// /// Interaction logic for PlayButton.xaml /// // PlayButton.xaml.cs public partial class PlayButton : System.Windows.Controls.UserControl { public PlayButton() { InitializeComponent(); } #region Fields RoutedEventHandler _mediaEndedHandler; bool _isPlaying = false; #endregion // Fields #region IconColor Property public static readonly DependencyProperty IconColorProperty = DependencyProperty.Register("IconColor", typeof(Brush), typeof(PlayButton), new FrameworkPropertyMetadata(Brushes.Black, new PropertyChangedCallback(OnIconColorChanged))); public Brush IconColor { get { return (Brush)GetValue(IconColorProperty); } set { SetValue(IconColorProperty, value); } } private static void OnIconColorChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) { // When the color changes, set the icon color PlayButton control = (PlayButton)obj; control.PlayIcon.Fill = control.IconColor; control.PauseIcon.Fill = control.IconColor; } #endregion // IconColor Property #region MediaPlayer Property /// /// The Dependency Property for the /// MediaPlayer property. /// public static readonly DependencyProperty MediaPlayerProperty = DependencyProperty.Register( "MediaPlayer", typeof(MediaElement), typeof(PlayButton), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnMediaPlayerChanged))); /// /// The ID of the Media Element that the button is tied to. /// public MediaElement MediaPlayer { get { return (MediaElement)GetValue(MediaPlayerProperty); } set { SetValue(MediaPlayerProperty, value); } } /// /// Static Event that is called when the property is changed. /// /// The object that has changed. /// The arguments to the property. private static void OnMediaPlayerChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) { PlayButton control = (PlayButton)obj; control.UpdateMediaPlayer(args.OldValue as MediaElement, args.NewValue as MediaElement); } #endregion // MediaPlayer Property #region Event Handlers /// /// Event Handler for when the Media Player has ended /// the media file. /// /// The Media Player /// The handler void Media_MediaEnded(object sender, RoutedEventArgs e) { MediaPlayer.Stop(); ChangePlayState(false); } /// /// Event Handler for when the PlayButton is Clicked /// /// The button /// The handler void PlayButton_Clicked(object sender, MouseButtonEventArgs e) { if (_isPlaying) { ChangePlayState(false); MediaPlayer.Pause(); } else { ChangePlayState(true); MediaPlayer.Play(); } } #endregion // Event Handlers #region Methods /// /// Used to register and unregister from MediaElements that the control are tied to. /// /// The Media Element (if any) to unregister event notification. /// The Media ELement on which to register event notification. void UpdateMediaPlayer(MediaElement oldMedia, MediaElement newMedia) { if (newMedia.LoadedBehavior != MediaState.Manual) { throw new NotSupportedException("Media Element's LoadedBehavior must be set to Manual to use this PlayButton"); } // Unregister if necessary if (_mediaEndedHandler != null && oldMedia != null) { oldMedia.MediaEnded -= _mediaEndedHandler; } // Register for the event if (newMedia != null) { // Create the handler if necessary if (_mediaEndedHandler == null) { _mediaEndedHandler = new RoutedEventHandler(Media_MediaEnded); } // Register for the Event newMedia.MediaEnded += _mediaEndedHandler; } } /// /// Changes the look of the control to show the right icon. /// /// Whether the new state will be 'playing' or 'paused'. void ChangePlayState(bool play) { _isPlaying = play; PlayIcon.Opacity = play ? 0 : 1; PauseIcon.Opacity = play ? 1 : 0; } #endregion // Methods } }