首先你要确定,你使用的是这个控件
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>