如何接受焦点C#WPF UserControl(How to accept focus C# WPF UserControl)

编程入门 行业动态 更新时间:2024-10-26 00:27:07
如何接受焦点C#WPF UserControl(How to accept focus C# WPF UserControl)

我正在尝试创建一个日期编辑UserControl,但使用TextBlock而不是TextBox,因为我想要一个输入上下文而不是管理六个可能的焦点。

我如何接受焦点? 我尝试将Focusable设置为TRUE,但似乎还不够。 还有什么我需要做的?

I'm trying to create a date edit UserControl, but using TextBlock rather than TextBox because I want a single input context rather than manage six possible foci.

How do I accept focus? I tried setting Focusable to TRUE but it appears not to be enough. What else do I have to do?

最满意答案

听起来你需要将KeyboardFocus设置为元素

例:

<UserControl x:Class="MyControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" FocusManager.IsFocusScope="True" FocusManager.FocusedElement="{Binding ElementName=mytextBlock}"> <Grid> <TextBox Name="mytextBlock" /> </Grid> </UserControl>

或者在codeBehind中:

FocusManager.SetFocusedElement(this, mytextblock); Keyboard.Focus(mytextblock);

Sounds like you need to set KeyboardFocus to the element

Example:

<UserControl x:Class="MyControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" FocusManager.IsFocusScope="True" FocusManager.FocusedElement="{Binding ElementName=mytextBlock}"> <Grid> <TextBox Name="mytextBlock" /> </Grid> </UserControl>

Or in codeBehind:

FocusManager.SetFocusedElement(this, mytextblock); Keyboard.Focus(mytextblock);

更多推荐

UserControl,TextBlock,TextBox,电脑培训,计算机培训,IT培训"/> <meta name="

本文发布于:2023-08-02 20:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1380015.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:焦点   WPF   UserControl   accept   focus

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!