gpt4 book ai didi

wpf - 将 ListBox 中的多个项目复制到剪贴板

转载 作者:行者123 更新时间:2023-12-04 04:39:20 30 4
gpt4 key购买 nike

我有一个 TextBlock 列表框,我希望最终用户能够从显示中复制文本,然后他们可以将其粘贴到他们喜欢的位置。我可以通过右键单击-> 复制以及用户的 ctrl+c 键获得单行副本。我还可以通过用户的 ctrl+c 按键获得多行副本。我希望能够使用右键单击-> 复制功能以及从菜单下拉调用中进行多行复制。

我的列表框:

    <!--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}") 但这仅适用于窗口窗体。

最佳答案

我只需要将我的代码复制到 RightClickCopyCmdExecuted进入 CallKeyDown() ,一切都很好。然后我会打电话CallKeyDown来自 RightClickCopyCmdExecuted .

我在列表框中右键单击-> 复制了多行。
所以我的 ListBox 变成了:

    <!--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}" />
</DataTemplate>
</ListBox.ItemTemplate>

<ListBox.ContextMenu>
<ContextMenu>
<MenuItem Command="Copy">
<MenuItem.CommandBindings>
<CommandBinding Command="ApplicationCommands.Copy" CanExecute="RightClickCopyCmdCanExecute" Executed="RightClickCopyCmdExecuted" />
</MenuItem.CommandBindings>
</MenuItem>
</ContextMenu>
</ListBox.ContextMenu>
</ListBox>

后面的代码变成:
    private void RightClickCopyCmdExecuted(object sender, ExecutedRoutedEventArgs e)
{
string collectedText = "";

foreach (DisplayData dd in Progress_Window.SelectedItems)
{
collectedText += dd._string + "\r\n";
}

if (Progress_Window.SelectedItems != null)
{
Clipboard.SetText(collectedText);
}
}

关于wpf - 将 ListBox 中的多个项目复制到剪贴板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19161397/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com