开发者

Rendering buttons on top of MediaElement in WPF

I have a MediaElement object in my XAML, which loads my mo开发者_StackOverflow社区vie 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?


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>


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]):

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜