I have found a way of putting arbitrary tooltips on DataGrid cells. It
relies heavily upon the VisualTreeHelper and some weak assumptions about the
control hierarchy. Here is a sample of the code with no optimisation or
validation and we assume that the DataGrid ItemsSource is a DataTable and
all columns are bound.
private void gridBrowser_MouseMove(object sender, MouseEventArgs e)
{
DataGridCell cell =
WpfUtility.FindParentControlType<DataGridCell>(e.OriginalSource);
DataGridRow row =
WpfUtility.FindParentControlType<DataGridRow>(e.OriginalSource);
DataGridBoundColumn bindcol = (DataGridBoundColumn)cell.Column;
Binding bind = (Binding)bindcol.Binding;
DataRowView drv = (DataRowView)row.Item;
object val = drv[bind.Path.Path];
if (oldcell != null)
{
oldcell.ToolTip = null;
}
cell.ToolTip = val;
oldcell = cell;
}
You have to walk up the tree to get the cell and row, then the binding path,
then the bound item for the row. Now you can extract the path's property
value from the data item.
I'm simply setting the tooltip to the cell value to prove it works, but I
will set it to a nice templated control. You have to remove the tip from a
previous cell before setting the current one. The helper method looks like
this:
public static T FindParentControlType<T>(object source) where T :
DependencyObject
{
DependencyObject dep = (DependencyObject)source;
while ((dep != null) && (!(dep is T)))
{
dep = VisualTreeHelper.GetParent(dep);
}
return (T)dep;
}
I just found a bug. If you mouse hover over a blank part of a cell that
contains a right justified number, then the upward tree walk gets a null
value because you're "off the value". Oh well, here we go again...
Greg
_______________________________________________
ozwpf mailing list
[email protected]
http://prdlxvm0001.codify.net/mailman/listinfo/ozwpf