WPF ListView - 如何复制单个单元格

发布于 2024-11-05 00:46:18 字数 1304 浏览 1 评论 0原文

我有代码可以查看 SelectedItem,然后输出 ToString() 以将记录放入剪贴板。

如何检测用户右键单击哪个单元格以便仅复制 SelectedItem 中的该单元格?

例如,如果我有借款人信息并且用户右键单击姓氏,我想提供将姓氏复制到剪贴板的功能。

谢谢你!

更新:

这是我按照乔什的建议使用的代码,效果很好:

  private void BorrowerInfoCopyClicked(object sender, RoutedEventArgs e)
        {
            BorrowerViewModel vm = this.DataContext as BorrowerViewModel;
            if (vm != null)
            {
                Clipboard.SetData(DataFormats.Text, vm.CurrentTextBlockText);
            }
        }

        private void AddressCopyClicked(object sender, RoutedEventArgs e)
        {
            BorrowerViewModel vm = this.DataContext as BorrowerViewModel;
            if (vm != null)
            {
                Clipboard.SetData(DataFormats.Text, vm.CurrentTextBlockText);
            }
        }

        private void lstViews_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            BorrowerViewModel vm = this.DataContext as BorrowerViewModel;
            if (vm != null)
            {
                if (e.OriginalSource is TextBlock)
                {
                    TextBlock txtBlock = e.OriginalSource as TextBlock;
                    vm.CurrentTextBlockText = txtBlock.Text;
                }

} }

I have code where I can take a look at the SelectedItem and then output ToString() to get the record into the clipboard.

How can I detect what cell the user is right clicking on in order to copy just that cell in the SelectedItem?

For example, if I have Borrower Information and the user right-clicks on last name, I would like to give the ability to just copy last name to clipboard.

Thank you!

UPDATE:

Here is the code that I used as suggested by Josh, it worked great:

  private void BorrowerInfoCopyClicked(object sender, RoutedEventArgs e)
        {
            BorrowerViewModel vm = this.DataContext as BorrowerViewModel;
            if (vm != null)
            {
                Clipboard.SetData(DataFormats.Text, vm.CurrentTextBlockText);
            }
        }

        private void AddressCopyClicked(object sender, RoutedEventArgs e)
        {
            BorrowerViewModel vm = this.DataContext as BorrowerViewModel;
            if (vm != null)
            {
                Clipboard.SetData(DataFormats.Text, vm.CurrentTextBlockText);
            }
        }

        private void lstViews_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            BorrowerViewModel vm = this.DataContext as BorrowerViewModel;
            if (vm != null)
            {
                if (e.OriginalSource is TextBlock)
                {
                    TextBlock txtBlock = e.OriginalSource as TextBlock;
                    vm.CurrentTextBlockText = txtBlock.Text;
                }

}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

意中人 2024-11-12 00:46:18

我通过处理 ListView 上的 PreviewMouseRightButtonDown 事件并检查 e.OriginalSource 是否是 TextBlock 来完成此操作。如果是这样,请将 txtBlk.Text 复制到剪贴板。此代码可以位于包含 ListView 的视图的代码隐藏中,也可以作为附加到 ListView 的行为。如果您需要使用上下文菜单来执行复制操作,请使用一个 TextBlock 字段来存储对 TextBlock 的引用,并在响应 MenuItem 的单击(或命令执行)的方法中引用那里的 TextBlock。

I've done this by handling the PreviewMouseRightButtonDown event on the ListView and checking if e.OriginalSource is a TextBlock. If so, copy the txtBlk.Text to the clipboard. This code could either be in the code-behind of the View that contains the ListView, or as a behavior you attach to the ListView. If you need to use a context menu to perform the Copy operation, have a TextBlock field that you use to store a reference to the TextBlock, and in your method that responds to a MenuItem's click (or Command execution) reference the TextBlock there instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文