Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering buttons on top of MediaElement in WPF

I have a MediaElement object in my XAML, which loads my movie just fine. What I want to do is render the play, pause and stop buttons right in the center my MediaElement object, kind of like how if you embed a YouTube video on a web page, you get the big play button in the center of the video. Can I do this in WPF using MediaElement?

like image 896
Icemanind Avatar asked Feb 23 '23 13:02

Icemanind


2 Answers

In case anyone else ever needs to do this, I figured it out. I simply created a Canvas and stuck the MediaElement and the Button both inside the Canvas. I then used ZIndex to change the ZOrdering so the button was on top. Finally, I centered the button on top of my movie. Here is the XAML:

<StackPanel Orientation="Horizontal" Background="LightGray" DockPanel.Dock="Bottom" Height="100">
        <Canvas Width="100">
            <Button Canvas.ZIndex="2" Canvas.Left="42" Canvas.Top="35">
                <Button.Template>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Polygon Points="0,0 0,26 17,13" Fill="#80000000" Stroke="White" />
                    </ControlTemplate>
                </Button.Template>
            </Button>
            <MediaElement Canvas.ZIndex="1" Canvas.Top="0" Canvas.Left="0" 
                          x:Name="mediaElement1" UnloadedBehavior="Manual" LoadedBehavior="Manual" ScrubbingEnabled="True" 
                          Loaded="mediaElement1_Loaded" Width="100" Height="100">
            </MediaElement>
        </Canvas>
    </StackPanel>
like image 58
Icemanind Avatar answered Feb 26 '23 02:02

Icemanind


Or you need to place mediaElement and Buttons to same Grid and set Canvas.ZIndex to higher number than 0:

    <Grid>
        <Button  VerticalAlignment="Center" HorizontalAlignment="Center" Height="50" Width="100" Canvas.ZIndex="1">Button</Button>
        <MediaElement Source="/Assets/ladybug.wmv">
         </MediaElement>
    </Grid>

(actually you don't need Canvas [works on UWP]):

like image 43
Alamakanambra Avatar answered Feb 26 '23 04:02

Alamakanambra