我有一个设置IsReadonly = True的WPF文本框。我想为这个文本框启用Ctrl+C,Ctrl+V &右键单击复制粘贴功能。在WPF文本框中有任何内置的功能吗?
发布于 2016-10-08 11:39:41
我使用了自定义ContextMenu显示Copy, Paste命令。
我处理过ContextMenuOpening / Closing events以向ContextMenu展示。如果我们不处理这些事件,ContextMenu就会出现,但是命令将被禁用。
我已经为PreviewKeyDown粘贴处理过KeyDown和Ctrl+V事件。
我已经将CaretBrush设置为Transparent,否则将在设置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>处理人员:
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;
}如果这能解决你手头的问题,请告诉我。
https://stackoverflow.com/questions/39922006
复制相似问题