WPF C1FlexGrid 扩展、带表头ID,并且有鼠标移到行高亮

首先你要确定,你使用的是这个控件

C1.WPF.FlexGrid


一、关于如何实现表头自带ID

对 CreateRowHeaderContent 函数重写


二、关于如何实现高亮鼠标行。

1、目前实现的是鼠标移动后台选中、离开后复原。本来要设置鼠标所在行Background,但是由于WPF控件特殊性,导致MouseOver覆盖掉了MouseDown跟其他事件

具体原因跟控件是否有 ‘绘画或内容‘ 区域有关。

2、高亮鼠标所在行算法,并不高明。从别人那弄来的。没时间改了。


三、关于前两个问题用代码一块贴出来,免得乱了


<span style="font-size:12px;color:#333333;">namespace DialogEx.Controls
{
    /// <summary>
    /// 表头RowID
    /// </summary>
    internal class DjzcCellFactory : CellFactory
    {
        public DjzcCellFactory()
        {

        }

        public override void CreateRowHeaderContent(C1FlexGrid grid, Border bdr, CellRange range)
        {
            if (range.Column == 0)
            {
                TextBlock textBlock = new TextBlock()
                {
                    HorizontalAlignment = HorizontalAlignment.Center,
                    VerticalAlignment = VerticalAlignment.Center,
                    Text = string.Format("{0}", range.Row + 1)
                };
                bdr.Padding = new Thickness(0);
                bdr.Child = textBlock;
            }
        }
    }

    public class C1FlexGridEx : C1FlexGrid
    {
       
        public static readonly DependencyProperty ShowRowIDProperty = DependencyProperty.Register("ShowRowID", typeof(bool), typeof(C1FlexGridEx), new PropertyMetadata(true));
        public static readonly DependencyProperty IsMouseMoveSelectedProperty = DependencyProperty.Register("IsMouseMoveSelected", typeof(bool), typeof(C1FlexGridEx), new PropertyMetadata(true));
        //public static readonly DependencyProperty MouseMoveSelectedBackgroudProperty = DependencyProperty.Register("MouseMoveSelectedBackgroud", typeof(Brush), typeof(C1FlexGridEx), new PropertyMetadata(Brushes.Transparent));
        
        /// <summary>
        /// 鼠标移动到的行
        /// </summary>
        private C1.WPF.FlexGrid.BoundRow MouseSelectRow;
        //private Brush MouseSelectRowBrush;

        /// <summary>
        /// 内容
        /// </summary>
        public bool ShowRowID
        {
            get { return (bool)GetValue(ShowRowIDProperty); }
            set { SetValue(ShowRowIDProperty, value); }
        }
        /// <summary>
        /// 是否开启鼠标移动选中背景色
        /// </summary>
        public bool IsMouseMoveSelected
        {
            get { return (bool)GetValue(IsMouseMoveSelectedProperty); }
            set { SetValue(IsMouseMoveSelectedProperty, value); }
        }
        /// <summary>
        /// 鼠标移动背景颜色
        /// </summary>
        //public Brush MouseMoveSelectedBackgroud
        //{
        //    get { return (Brush)GetValue(MouseMoveSelectedBackgroudProperty); }
        //    set { SetValue(MouseMoveSelectedBackgroudProperty, value); }
        //}


        public C1FlexGridEx()
        {
            </span><span style="font-size:12px;color:#ff0000;">if (ShowRowID == true)
            {
                this.CellFactory = new DialogEx.Controls.DjzcCellFactory();
            }</span><span style="font-size:12px;color:#333333;">
        }

        protected override void OnMouseMove(System.Windows.Input.MouseEventArgs EventArgs)
        {
            if (IsMouseMoveSelected == true)
            {
                try
                {
                    int row = this.Selection.Row;
                    int column = this.Selection.Column;
                    int MouseRow = -1;
                    int MouseCol = -1;
                    double actualHeight = this.ColumnHeaders.ActualHeight;
                    double actualWidth = this.RowHeaders.ActualWidth;
                    Point position = EventArgs.GetPosition(this);

                    int num2 = 0;
                    while (true)
                    {
                        if (num2 < this.Rows.Count)
                        {
                            actualHeight = actualHeight + this.Rows[num2].ActualHeight;
                            if (position.Y - this.ScrollPosition.Y < actualHeight)
                            {
                                MouseRow = num2;
                                break;
                            }
                            else
                            {
                                num2++;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                    int num3 = 0;
                    while (true)
                    {
                        if (num3 < this.Columns.Count)
                        {
                            actualWidth = actualWidth + this.Columns[num3].ActualWidth;
                            if (position.X - this.ScrollPosition.X < actualWidth)
                            {
                                MouseCol = num3;
                                break;
                            }
                            else
                            {
                                num3++;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                    if ((row != MouseRow || column != MouseCol) && MouseRow != -1 && MouseCol != -1)
                    {
                        if (this.Rows[MouseRow].Selected == false)
                        {
                            //this.Select(MouseRow, MouseCol, true);
                            if (MouseSelectRow != null)
                            {
                                MouseSelectRow.Selected = false;
                                //MouseSelectRow.Background = MouseSelectRowBrush;
                            }
                            MouseSelectRow = this.Rows[MouseRow] as C1.WPF.FlexGrid.BoundRow;
                            //MouseSelectRowBrush = MouseSelectRow.Background;
                            MouseSelectRow.Selected = true;//.Background = MouseMoveSelectedBackgroud;
                        }
                    }
                    else if ((MouseRow < 0 || MouseCol < 0) && MouseSelectRow != null)
                    {
                        MouseSelectRow.Selected = false;
                    }
                }
                catch (Exception exception)
                {
                }
            }
            EventArgs.Handled = false;
            base.OnMouseMove(EventArgs);
        }

        protected override void OnMouseLeave(System.Windows.Input.MouseEventArgs EventArgs)
        {
            if (MouseSelectRow != null)
            {
                MouseSelectRow.Selected = false;
            }
            base.OnMouseLeave(EventArgs);
        }
    }
}</span><span style="color:#ff0000;">
</span>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值