渲染到离屏缓冲区时 WPF 文本框焦点
我正在使用 RenderTargetBitmap 将文本框渲染到屏幕外缓冲区,并且我想在该文本框中输入文本,但是,我无法设置文本框焦点(使用 Focus() 没有效果 - 可能是因为画布没有附加到真实的画布上)窗口?)这有两个后果: 1 键盘输入不会被捕获 2. 即使我使用 TextBox 文本和 CaretIndex = index 手动设置文本,由于缺乏焦点,我也不会得到插入符号。有没有办法以某种方式集中注意力?如果没有,我希望至少让插入符号可见,即使没有焦点,但看不到这样做的方法。
i am rendering a textbox to an offscreen buffer using RenderTargetBitmap and i want to enter text into this textbox, however, i can't set the textbox focus (using Focus() has no effect - presumably because the canvas is not attached to a real window?) this has two consequenes: 1 the keyboard input doesn't get captured 2. even if i set the text manually using TextBox text and CaretIndex = index i dont get a caret because of the lack of focus. is there a way to make this focus somehow? and if not, i'd like to at least have the caret visible even if there is no focus, but can't see a way of doing this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将其托管在真实窗口中才能正常工作,因为 Windows 只会将键盘输入直接传递到活动窗口。您还没有明确说明为什么不希望您的
TextBox
托管在真实窗口中,因此很难知道您期望如何捕获键盘输入 - 您能否详细说明一下您的场景?从上下文中我不得不猜测,在您的场景中确保原始文本框不可见非常重要。实际上,您可以将文本框放置在窗口中,使其保持活动状态并能够接收所有形式的输入,而实际上并不可见。这是一种方法:
这里的
TextBox
元素本身非常普通,但我将其放入Grid
中,通过设置其Opacity< 使其不可见/code> 为 0。出于命中测试和键盘输入目的,它仍然被认为是可见的。由于我已将
Opacity
应用于包含的Grid
,而不是TextBox
本身,因此您可以将其与一起使用>渲染目标位图
。这是代码隐藏中的文本更改事件处理程序:最后一行仅用于验证目的 - 我使用带有画笔的图像来绘制矩形以检查它看起来是否应该。我的主 Xaml 还包含以下内容:
我的代码隐藏构造函数包含以下内容:
当我开始输入时,我可以看到文本框的(由于大小不匹配而扭曲)位图副本出现在矩形中,验证我能够获取文本框的位图副本,其中包含文本输入、焦点、插入符号以及所有内容。
文本框本身保持“屏幕外”状态,即它保持不可见。
由于我不清楚你实际上想做什么,我不知道这是否有帮助,但我希望它有用。
You will need the thing to be hosted in a real window for this to work because Windows will only deliver keyboard input directly to the active window. You've not made it clear why you want your
TextBox
not to be hosted in a real window, so it's difficult to know how you expect keyboard input to be captured - could you elaborate on your scenario a little?From the context I have to guess that it's important in your scenario to ensure that the original text box is not visible. You can actually put a text box in a window in such a way that it remains active and able to receive all forms of input without actually being visible. Here's one way:
The
TextBox
element itself here is perfectly ordinary, but I've put it inside aGrid
that I've made invisible by setting itsOpacity
to 0. For hit testing and keyboard input purposes, it's still considered to be visible. And because I've applied theOpacity
to the containingGrid
, and not theTextBox
itself, you are free to use it with aRenderTargetBitmap
. Here's my text change event handler in the codebehind:That last line is just for verification purposes - I'm using the image with a brush to paint a rectangle to check it looks like it should. My main Xaml also contains this:
My codebehind's constructor contains this:
And when I start typing, I can see a (distorted, due to the mismatched sizes) bitmap copy of my text box appear in the rectangle, verifying that I'm able to get a bitmap copy of the text box, with text input, focus, caret, and everything.
The text box itself remains 'off screen' in the sense that it remains invisible.
Since it's not clear to me what you're actually trying to do, I've no idea if this helps at all, but I hope it's useful.