如何接受焦点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="
发布评论