none
wpf: MediaElement as a custom control

    Question

  • Hi,

    I've implemented a custom control which has several controls related media element (play/pause button, timer, forward,backward, etc...)

    also i've embedded a media element control to my custom control...

    when i use my custom control, how can i access to the embedded media element control in order to set the source property ?

     

    thanks

    Sunday, April 24, 2011 4:41 PM

Answers

  • Hello, try the above code (put a CustomMediaElement in your Window/Page) and set the CustomMediaElement Source property to the desired file location (with or without binding).

    See you,

    Miguel.

    • Marked as answer by igalep132 Tuesday, April 26, 2011 1:42 PM
    Monday, April 25, 2011 12:31 PM

All replies

  • Hello, you need to create a new Custom Control and register a new dependency property called Source to bind:

    Xaml code for the CustomMediaElement:

    <UserControl x:Class="WpfApplication1.CustomMediaElement"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
           mc:Ignorable="d" 
           d:DesignHeight="300" d:DesignWidth="300" Name="CustomControl">
      <DockPanel>
        <MediaElement Source="{Binding Path=Source, ElementName=CustomControl}"/>
      </DockPanel>
    </UserControl>
    
    

    C# code behind for the CustomMediaElement:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    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 WpfApplication1
    {
      public partial class CustomMediaElement : UserControl
      {
        public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof(Uri), typeof(CustomMediaElement));
    
        public CustomMediaElement()
        {
          this.InitializeComponent();
        }
    
        public Uri Source
        {
          get
          {
            return (Uri)this.GetValue(SourceProperty);
          }
    
          set
          {
            this.SetValue(SourceProperty, value);
          }
        }
      }
    }
    
    

    Hope this helps,

    Miguel.

     

     

     

    Sunday, April 24, 2011 6:27 PM
  • thanks for the answer,

    it means that i cant bind my media file location to the media element source ? but i need to use Source property instead ?

    Monday, April 25, 2011 6:58 AM
  • Hello, try the above code (put a CustomMediaElement in your Window/Page) and set the CustomMediaElement Source property to the desired file location (with or without binding).

    See you,

    Miguel.

    • Marked as answer by igalep132 Tuesday, April 26, 2011 1:42 PM
    Monday, April 25, 2011 12:31 PM
  • Hi igalep132,

    You could bind it follow Migel's solution, I think this is a good way to resolve your issue.

    On the other hand, if you just want to get the MediaElement, you could try to use the FindName method, and then you could reset the source property.

     

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, April 26, 2011 5:57 AM
  • worked like a charm :)

    Thanks a lot

    Tuesday, April 26, 2011 1:43 PM