在 Silverlight 5.0 中设置字体样式时有一个限制,我们无法将字体设置为 FontStyle.Strikeout 样式,本文主要演示如何在 C1FlexGrid for Silverlight 中实现这样的功能。本文通过在单元格中通过画线的方法来实现字体删除线样式。

最关键的操作就是通过自定义的CellFactory来自己绘制单元格内容,代码如下:

    public class StrikeoutCellFactory : C1.Silverlight.FlexGrid.CellFactory
    {
        public override void CreateCellContent(C1.Silverlight.FlexGrid.C1FlexGrid grid, System.Windows.Controls.Border bdr, C1.Silverlight.FlexGrid.CellRange rng)
        {
            base.CreateCellContent(grid, bdr, rng);
            if (rng.Column == 0)
            {
                var obj = bdr.Child;
                if (obj.GetType().Equals(typeof(TextBlock)))
                {
                    if (!(((TextBlock)obj).Text == string.Empty))
                    {
                        bdr.Child = null;
                        double width = 0;
                        double height = 0;
                        if ((TextBlock)obj != null)
                        {
                            width = ((TextBlock)obj).ActualWidth ;
                            height = ((TextBlock)obj).ActualHeight;
                        }
                        Line ln = new Line();
                        ln.Stroke = new SolidColorBrush(Colors.Red);
                        ln.StrokeThickness = 1;
                        ln.X1 = 0;
                        ln.Y1 = (height / 2) + 3;
                        ln.X2 = width + 1;
                        ln.Y2 = (height / 2) + 3;
                        Grid grd = new Grid();
                        grd.Children.Add(ln);
                        grd.Children.Add(obj);
                        ((TextBlock)obj).Foreground = new SolidColorBrush(Colors.Green);
                        bdr.Child = grd;
                    }
                }
            }
        }
    }

 

 

运行截图:

C1FlexGrid for Silverlight

 

源码下载:VS2010 + C1 Studio for Silverlight 2013V1