通过UIElement获取鼠标单击UltraGridCell & 给UltraGridCell加ToolTip

Infragistic 的UltraGrid控件不提供单击cell的事件,但有时候我们又需要这个事件,所以就只好自己写了:

  1. 使用UltraGrid的MouseClick事件,这样可以取得单击的Cell,然后就可以对Cell做需要的操作
  2. MouseClick代码
     1private void ultraGrid1_MouseClick(object sender, MouseEventArgs e)
     2{
     3    UIElement mainElement = ((IUltraControlElement)UltraGrid1).MainUIElement;
     4    UltraGridCell cell= null;
     5    //you can define what you want , such as UltraGridCell, UltraGridRow, UltraGridColumn or ColumnHeader
     6    //UltraGridRow row=null;                                                                       
     7    UIElement element = mainElement.ElementFromPoint(new Point(e.X, e.Y));                           
     8    while (element != null && cell== null)//replace cell with row,column or column header
     9    {
    10        cell= PrepareCell(element);
    11        //row = PrepareRow(element);
    12        if (cell== null//replace cell with row,column or column header
    13            element = element.Parent;
    14    }

    15    if(cell == null)
    16        return;
    17    //here you get the cell you need, then you can do any thing you want
    18}
  3. PrepareCell代码
     1//use the same way to get UltraGridRow or UltraGridColumn, etc.
     2UltraGridCell PrepareCell(UIElement element) 
     3{
     4    //cast the UIElement to the specified element you want
     5    CellUIElement cellElement = element as CellUIElement ;
     6    if (cellElement == null)
     7        return null;
     8    //get the context from the element
     9    UltraGridCell cell= cellElement .GetContext(typeof(UltraGridCell)) as UltraGridCell;
    10    return cell;
    11}
  4. 当需要给UltraGridCell 加ToolTip时也可以用相同的办法实现,不过是要实现MouseMove and MouseLeave事件
posted @ 2007-08-20 14:38  Junde  阅读(904)  评论(5编辑  收藏  举报