开发者

WPF texbox focus when render to offscreen buffer

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 c开发者_Go百科aret 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.


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:

<Grid>
    <Grid
        x:Name="container"
        HorizontalAlignment="Left" VerticalAlignment="Top"
        Margin="69,42,0,0"
        Opacity="0"
        >
        <TextBox
            Name="textBoxToRender"
            Height="23" Width="120"
            TextChanged="textBoxToRender_TextChanged"
            />
    </Grid>

</Grid>

The TextBox element itself here is perfectly ordinary, but I've put it inside a Grid that I've made invisible by setting its Opacity to 0. For hit testing and keyboard input purposes, it's still considered to be visible. And because I've applied the Opacity to the containing Grid, and not the TextBox itself, you are free to use it with a RenderTargetBitmap. Here's my text change event handler in the codebehind:

private void textBoxToRender_TextChanged(object sender, TextChangedEventArgs e)
{
    var rtb = new RenderTargetBitmap(
        (int) textBoxToRender.ActualWidth, (int) textBoxToRender.ActualHeight,
        96, 96, PixelFormats.Pbgra32);

    rtb.Render(textBoxToRender);

    rectangle1.Fill = new ImageBrush(rtb);
}

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:

<Rectangle Height="72" HorizontalAlignment="Left" Margin="74,167,0,0"
   Name="rectangle1" Stroke="Black" VerticalAlignment="Top" Width="255" />

My codebehind's constructor contains this:

textBoxToRender.Focus();

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜