Silverlight 3中的剪贴板支持 [英] Clipboard Support in Silverlight 3

查看:75
本文介绍了Silverlight 3中的剪贴板支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究开发在DataGrid中显示很多信息的Silverlight应用程序。

I'm looking at developing a Silverlight application that displays a lot of information in a DataGrid.

我想以某种方式用户可以通过剪贴板将其复制到Excel中。

I want to somehow give the users the ability to copy this into Excel via the clipboard.

在Silverlight 3中甚至有可能吗?

Is this even possible in Silverlight 3?

推荐答案

好,我想出了如何做到的,但是它并不十分优雅。

OK, I've figured out how to do it, but it's not exactly elegant.

首先,我从 Jeff Wilcox的博客

现在,我已经编写了从网格生成HTML表并将其放入剪贴板的代码。

Now I've written code to generate an HTML table from the grid, and put that into the clipboard.

    private void Clipboard_Button_Clicked(object sender, RoutedEventArgs e)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<TABLE>");
        foreach (object obj in myDataGrid.ItemsSource)
        {
            sb.Append("<TR>");
            foreach (DataGridColumn c in myDataGrid.Columns)
            {
                sb.Append("<TD>");
                FrameworkElement el = c.GetCellContent(obj);
                TextBlock tb = el as TextBlock;
                if (tb != null)
                {
                    string s = tb.Text;
                    sb.Append(System.Windows.Browser.HttpUtility.HtmlEncode(tb.Text));
                }
                sb.Append("</TD>");
            }
            sb.Append("</TR>");
        }
        sb.Append("</TABLE>");
        Clipboard.SetText(sb.ToString()); 
    }

特别糟糕,因为它正在呼叫

It's especially bad because it's calling

clipboardData.Invoke("setData", "text", text);

而不是

clipboardData.Invoke("setData", "text/html", text);

因为第二个抛出 System.InvalidOperation异常。这意味着,如果将其复制到Word中而不是Excel中,它不是表,而是HTML块。

Because the second one throws a "System.InvalidOperation" exception. That means if you copy it into Word instead of Excel it isn't a table, it's a block of HTML.

但是,是的,可以通过以下方式将datagrid内容复制到Excel中:剪贴板是可能的。

But, yes, copying the datagrid contents to Excel via the clipboard is possible. Sort of.

这篇关于Silverlight 3中的剪贴板支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆