Reputation: 5771
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?
Upvotes: 2
Views: 5029
Reputation: 9827
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.
Upvotes: 0
Reputation: 3221
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.
Upvotes: 1