Home > database >  Override MediaElement on top of Webview2
Override MediaElement on top of Webview2

Time:12-16

I created a WPF project in which I need display two StackPanels like this:

Two StackPanels: One yellow StackPanel (stackPanel1) with a red StackPanel (stackPanel2 on top of it in its left half with a margin around.

This screen is child of main Grid

StackPanel 1 will be displayed without any alternation of width and height and I need to display the entire second StackPanel on top of previous StackPanel. Using Canvas and DockPanel doesn't help me in this case.

<Grid>

    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    
    <StackPanel VerticalAlignment="Center" Grid.Row="0" x:Name="web1">
        <wv2:WebView2 Name="MyWebView"
             Height="800" >
        </wv2:WebView2>
    </StackPanel>

    <StackPanel Grid.Row="0" x:Name="videoPlayerPanel">
        <Canvas >
            <local:VideoPlayer x:Name="videoPlayer"  Background="AntiqueWhite"/>
        </Canvas>
    </StackPanel>

</Grid>

CodePudding user response:

I agree with @thatguy answer , if we use the zIndex we can overlap the element in WPF, But the question here is related to webview2 and mediaElement. As of now webview2 not allow another element to come on top. So there is no way to display your mediaElement on top of webview as of now. Reffer to this Link for this issue : Webview2 issue

You can try to display mediaElement using popup but this will give other problems I think this is the only answer to your question right now .

CodePudding user response:

The order in which you define the StackPanels inside the Grid matters. The next item is displayed above the previous one. In your example, videoPlayerPanel will be above web1, because it is defined after web1. There are two options to alter this behavior.

  1. Change the order, define videoPlayerPanel before web1, to display web1 above videoPlayerPanel.

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" x:Name="videoPlayerPanel">
            <Canvas >
                <local:VideoPlayer x:Name="videoPlayer"  Background="AntiqueWhite"/>
            </Canvas>
        </StackPanel>
        <StackPanel VerticalAlignment="Center" Grid.Row="0" x:Name="web1">
            <wv2:WebView2 Name="MyWebView"
                 Height="800" >
            </wv2:WebView2>
        </StackPanel>
    </Grid>
    
  2. Define the Z-Order explicitly by setting a ZIndex for the items.

    Gets or sets a value that represents the order on the z-plane in which an element appears. [...] The greater the value of a given element, the more likely the element is to appear in the foreground.

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <StackPanel VerticalAlignment="Center" Grid.Row="0" x:Name="web1" ZIndex="1">
            <wv2:WebView2 Name="MyWebView"
                 Height="800" >
            </wv2:WebView2>
        </StackPanel>
        <StackPanel Grid.Row="0" x:Name="videoPlayerPanel">
            <Canvas >
                <local:VideoPlayer x:Name="videoPlayer"  Background="AntiqueWhite"/>
            </Canvas>
        </StackPanel>
    </Grid>
    

The default ZIndex is zero, and that explains why the order in the Grid matters.

Members of a Children collection that have equal ZIndex values are rendered in the order in which they appear in the visual tree. You can determine the index position of a child by iterating the members of the Children collection.

  • Related