2

I have a WPF textbox where I have set the IsReadonly = True. I want to enable the Ctrl+C, Ctrl+V & Right Click Copy-Paste features for this textbox. Is there any inbuilt functionality within WPF textbox for this?

1
  • 5
    Ctrl+C and Right Click Copy already works. As for paste - textbox is read only, so how you suppose paste to work? Commented Oct 7, 2016 at 16:34

2 Answers 2

1

If I understand your question correctly, you want to allow only copy/paste but no other text inputs. Maybe you want to enable the keyboard navigation/selection as well so that the user can navigate around and select some text.

To accomplish that you have to remove the IsReadOnly = True and set the following EventHandler:

<TextBox PreviewKeyDown="TextBox_PreviewKeyDown">

The code behind then looks like this:

private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
    switch (e.Key)
    {
        // Enable copy/paste and selection of all text.
        case Key.C:
        case Key.V:
        case Key.A:
            if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
                return;
            break;

        // Enable keyboard navigation/selection.
        case Key.Left:
        case Key.Up:
        case Key.Right:
        case Key.Down:
        case Key.PageUp:
        case Key.PageDown:
        case Key.Home:
        case Key.End:
            return;
    }
    e.Handled = true;
}

To disable the Cut entry in the right click context menu you have to set a custom ContextMenu as well:

<TextBox PreviewKeyDown="TextBox_PreviewKeyDown">
    <TextBox.ContextMenu>
        <ContextMenu>
            <MenuItem Command="ApplicationCommands.Copy" />
            <MenuItem Command="ApplicationCommands.Paste" />
        </ContextMenu>
    </TextBox.ContextMenu>

This should enable the Ctrl+C, Ctrl+V and right click copy/paste features but disable all other inputs.

Sign up to request clarification or add additional context in comments.

1 Comment

This does it! I also added Key.Back and Key.Delete to the case structure.
0

I haved used custom ContextMenu showing Copy, Paste commands.

I have handled ContextMenuOpening / Closing events to show ContextMenu. If we dont handle these events, ContextMenu will appear but commands would be disabled.

I have handled PreviewKeyDown and KeyDown events for Ctrl+V paste.

I have set CaretBrush to Transparent, otherwise it will appear when we set IsReadOnly = true.

<TextBox PreviewKeyDown="TextBox_PreviewKeyDown_1" KeyDown="TextBox_KeyDown_1" ContextMenuOpening="TextBox_ContextMenuOpening_1" ContextMenuClosing="TextBox_ContextMenuClosing_1" IsReadOnly="True" Text="a" CaretBrush="Transparent" Background="Bisque" Width="277">
    <TextBox.ContextMenu>
        <ContextMenu>
            <MenuItem Command="ApplicationCommands.Copy" />
            <MenuItem Command="ApplicationCommands.Paste" />
        </ContextMenu>
    </TextBox.ContextMenu>
</TextBox>

Handlers :

private void TextBox_ContextMenuOpening_1(object sender, ContextMenuEventArgs e)
{
    (sender as TextBox).IsReadOnly = false;
}

private void TextBox_ContextMenuClosing_1(object sender, ContextMenuEventArgs e)
{
    (sender as TextBox).IsReadOnly = true;
}   

private void TextBox_PreviewKeyDown_1(object sender, KeyEventArgs e)
{
    if (e.Key == System.Windows.Input.Key.V && Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
    {
        (sender as TextBox).IsReadOnly = false;
    }
}

private void TextBox_KeyDown_1(object sender, KeyEventArgs e)
{
    (sender as TextBox).IsReadOnly = true;
}

Please tell if this solves your issue at hand.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.