我有一個TextBlock的列表框,我希望最終用戶能夠從顯示中複製文本,然後將其粘貼到他們喜歡的位置。我可以通過右鍵單擊 - >複製,並用用戶的ctrl + c鍵按下來獲得單行副本。我也可以通過用戶的ctrl + c鍵按下多行復制。我希望能夠通過右鍵單擊 - >複製功能以及從菜單下拉菜單中進行多行復制。將ListBox中的多個項目複製到ClipBoard
我的列表框:
<!--Progress Window-->
<ListBox x:Name="Progress_Window" ItemsSource="{Binding _displayString}" ScrollViewer.ScrollChanged="Progress_Window_ScrollChanged" KeyDown="Progress_Window_KeyDown" SelectionMode="Extended">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding _string}" Foreground="{Binding _color}" FontSize="{Binding _fontSize}">
<TextBlock.ContextMenu>
<ContextMenu>
<MenuItem Command="Copy">
<MenuItem.CommandBindings>
<CommandBinding Command="ApplicationCommands.Copy" CanExecute="RightClickCopyCmdCanExecute" Executed="RightClickCopyCmdExecuted" />
</MenuItem.CommandBindings>
</MenuItem>
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
的背後代碼:
[DllImport("user32.dll", SetLastError = true)]
static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
[DllImport("kernel32.dll")]
static extern bool GenerateConsoleCtrlEvent(uint dwCtrlEvent, uint dwProcessGroupId);
public const int KEYEVENTF_EXTENDEDKEY = 0x0001; //Key down flag
public const int KEYEVENTF_KEYUP = 0x0002; //Key up flag
public const int VK_LCONTROL = 0xA3; //0xA2; //Left Control key code
public const int C = 0x43; //A Control key code
private void Progress_Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.C && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
{
try
{
ListBox lb = (ListBox)(sender);
string collectedText = "";
foreach (DisplayData dd in lb.SelectedItems)
{
collectedText += dd._string + "\r\n";
}
if (lb.SelectedItems != null)
{
Clipboard.SetText(collectedText);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void RightClickCopyCmdExecuted(object sender, ExecutedRoutedEventArgs e)
{
MenuItem mi = (MenuItem)sender;
DisplayData selected = (DisplayData)mi.DataContext;
if (selected != null)
{
Clipboard.SetText(selected._string);
}
}
private void RightClickCopyCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
public void CallKeyDown()
{
//Progress_Window.Focus();
//// Hold Control down and press C
//keybd_event(VK_LCONTROL, 0, 0, 0);
//keybd_event(C, 0, 0, 0);
//keybd_event(C, 0, KEYEVENTF_KEYUP, 0);
//keybd_event(VK_LCONTROL, 0, KEYEVENTF_KEYUP, 0);
System.Windows.Forms.SendKeys.SendWait("^{c}");
var key = Key.C; // Key to send
var target = Progress_Window; // Target element
var routedEvent = Keyboard.KeyDownEvent; // Event to send
target.RaiseEvent(new KeyEventArgs(Keyboard.PrimaryDevice, PresentationSource.FromVisual(target), 0, key) { RoutedEvent = routedEvent });
}
}
從菜單下拉:
<Grid>
<Menu>
<!--File-->
<MenuItem Header="_File">
<MenuItem Header="_Close" Command="{Binding Close}" />
</MenuItem>
<!--Edit-->
<MenuItem Header="_Edit">
<MenuItem Header="_Copy Ctrl+C" Command="{Binding CtrlC}" />
</MenuItem>
CTRLC命令調用觸發CallKeyDown一個Action()。
我試圖將我的ContextMenu從TextBlock移動到ListBox,但是這給了我ViewModel的DataContext,而不是給我_displayString數據。你可以在CallKeyDown()中看到我試圖模擬一個ctrl + c鍵從代碼後面按下沒有成功。我不知道這是否是解決此問題的最佳方法。我也嘗試使用SendKeys.Send(「^ {c}」),但只適用於窗體窗體。
什麼問題? – Nitin
當我給你打字時,我發現了它。我只需要將我的代碼複製到CallKeyDown()的RightClickCopyCmdExecuted中,一切都很好。然後我會從RightClickCopyCmdExecuted調用CallKeyDown。 – Bluto