← 返回所有博客文章

Spread for WPF-Silverlight 可以定制样式丰富的数据格式,在本篇文章中我们将阐述如何在单元格中添加超链接。

具体实现方法如下:

1. 继承 CustomDrawingObject 类创建自定义绘制元素:

public class ControlDrawingObject : CustomDrawingObject
{
    private Control _rootElement;
    public ControlDrawingObject(int row, int col, Control control) : base(row, col) { _rootElement = control; this.ShowDrawingObjectOnly = true; }
    public override FrameworkElement RootElement
    {
        get { _rootElement.Margin = new Thickness(1); return _rootElement; }
    }
}

 

2. 使用 DrawingObject 创建超链接按钮,继承 WorkSheet 类创建自定义表单

public class MyWorksheet : Worksheet
{
    public bool DrawingObjectVisible { get; set; }
    public override DrawingObject[] GetDrawingObject(int row, int column, int rowCount, int columnCount)
    {
        if (row != 1 || column != 1) return base.GetDrawingObject(row, column, rowCount, columnCount);
        DrawingObject dobj;
        dobj = new ControlDrawingObject(row, column, new HyperlinkButton() { Content = "This is a link", NavigateUri = new Uri("http://www.google.com") });
        return new DrawingObject[] { dobj };
    }
}

 

3. 最后,添加自定义表单到 GcSpreadSheet

this.gcSpreadSheet1.Sheets.Clear();
this.gcSpreadSheet1.Sheets.Add(new MyWorksheet());
var sheet = this.gcSpreadSheet1.ActiveSheet;

 

示例Demo:

 

下载试用版体验更多更能:点击下载