简体   繁体   中英

Set focus to UserControl (c# UWP)

I have a problem setting focus to my UserControl. I want the control to gain focus whenever it is clicked (pointer pressed event).

Here's the control:

<UserControl
x:Class="MyApp.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="100"
d:DesignWidth="160"
Canvas.ZIndex="10"  
IsEnabled="True"
IsFocusEngagementEnabled="True"
AllowFocusOnInteraction="True"
UseSystemFocusVisuals="True">
...
</UserControl>

Whenever pointer is pressed I call this.Focus(FocusState.Pointer) in the code. However the method always returns false (fails to set FocusState, which remains as Unfocused). What should I do to set focus to my user control?

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