简体   繁体   中英

WPF allow child user control to control specific keypress events of its parent

Using WPF.Net5. I have 3 user controls. VideoHost is a user control I made that hosts two separate child user controls I made called VideoControl & MediaElement. Videocontrol has buttons and slider that control the actions of MediaElement. The buttons on Videocontrol have custom routed events that bubble up to VideoHost where they perform actions on MediaElement like Play stop pause etc. My problem is I also want certain keypress functions to perform actions on MediaElement. Such as the Arrow keys. I want to put the code for keypress events in VideoControl, but it only works when VideoControl has the focus. How can I keep the code for Keypress inside VideoControl, allowing a hook-up in VideoHost via XML tags/properties/events that will deal with specific keys being pressed regardless if VideoControl has the focus or not.

VideoHost is a user control I made that hosts two separate child user controls I made called VideoControl & MediaElement

Based on this description, your .xaml is similar to this..

<UserControl x:Class="VideoHost" KeyDown="VideoHost_OnKeyDown">
    <StackPanel Orientation="Vertical">
        <views:MediaElement x:Name="MediaElement"/>
        <views:VideoControl x:Name="VideoControl"/>
    </StackPanel>
</UserControl x:Class="VideoHost">

You can handle KeyDown event in VideoHost and invoke the functions that are in VideoControl

private void VideoHost_OnKeyDown(object sender, KeyEventArgs e)
{
    // MediaElement.InvokeMyKeypressCode(e);
    VideoControl.InvokeMyKeypressCode(e);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM