Display File Name in ToolTip
Environment
| Product Version | 2022.2.511 |
| Product | RadFileDialogs for WPF |
Description
How to display a tooltip that contains the name of a file.
Solution
The files in the Extra large icons, Large icons, Medium icons, Small icons, List and Tiles views, are represented by a RadListBoxItem element. To
display a tooltip for this element, register two new event handlers for the RadListBoxItem class, via the EventManager.RegisterClassHandler method. These two handlers will be for the MouseEnter and MouseLeave events.
Register new MouseEnter and MouseLeave event handlers
EventManager.RegisterClassHandler(typeof(RadListBoxItem), RadListBoxItem.MouseEnterEvent, new MouseEventHandler(GetOnMouseEnter), true);
EventManager.RegisterClassHandler(typeof(RadListBoxItem), RadListBoxItem.MouseLeaveEvent, new MouseEventHandler(GetOnLeaveEnter), true);To reach the name of a file or a directory, cast the Content property of the RadListBoxItem element to either FileInfoWrapper or DirectoryInfoWrapper type. The FileInfoWrapper class contains information for the files and DirectoryInfoWrapper class-for directories. The name for the file or directory can be retrieved, through the Name property of both of these classes.
Cast the Content property and retrieve the file/directory name
private static void GetOnMouseEnter(object sender, MouseEventArgs e)
{
RadListBoxItem item = (RadListBoxItem)sender;
if (item.Content is FileInfoWrapper)
{
FileInfoWrapper infoWrapper = item.Content as FileInfoWrapper;
string fileName = infoWrapper.Name;
}
else if (item.Content is DirectoryInfoWrapper)
{
DirectoryInfoWrapper directoryInfoWrapper = item.Content as DirectoryInfoWrapper;
string directoryName = directoryInfoWrapper.Name;
}
}Create a ToolTip class instance and set its Content property to the name of the file/directory. Then, set this instance to the ToolTip property of the RadListBoxItem element.
Create and set ToolTip for RadListBoxItem
private static void GetOnMouseEnter(object sender, MouseEventArgs e)
{
RadListBoxItem item = (RadListBoxItem)sender;
if (item.Content is FileInfoWrapper)
{
FileInfoWrapper infoWrapper = item.Content as FileInfoWrapper;
string fileName = infoWrapper.Name;
if (item.ToolTip != null)
{
(item.ToolTip as ToolTip).IsOpen = true;
}
else
{
var tooltip = new ToolTip() { Content = fileName };
item.ToolTip = tooltip;
tooltip.IsOpen = true;
}
}
else if (item.Content is DirectoryInfoWrapper)
{
DirectoryInfoWrapper directoryInfoWrapper = item.Content as DirectoryInfoWrapper;
string directoryName = directoryInfoWrapper.Name;
if (item.ToolTip != null)
{
(item.ToolTip as ToolTip).IsOpen = true;
}
else
{
var tooltip = new ToolTip() { Content = directoryName };
item.ToolTip = tooltip;
tooltip.IsOpen = true;
}
}
}To control the delay of the initial display of the tooltip, you can use the RadToolTipService.SetInitialShowDelay method.
Set initial delay
RadListBoxItem item = (RadListBoxItem)sender;
var delay = RadToolTipService.GetInitialShowDelay(item);
if (delay != 0)
{
RadToolTipService.SetInitialShowDelay(item, 0);
}Inside the registered handler for the MouseLeave event, set the IsOpen property of the ToolTip property to False, to close the tooltip.
Close the tooltip
private void GetOnLeaveEnter(object sender, MouseEventArgs e)
{
RadListBoxItem item = (RadListBoxItem)sender;
if ((item.ToolTip as ToolTip).IsOpen)
{
(item.ToolTip as ToolTip).IsOpen = false;
}
}