0
votes

I have a ListView with pretty long listelement at times. I would like to create an event, where if I drag the mouse over an element, the whole name appears in a tooltip-like small window with the whole text of the item. This way the user can read it even if it is too long for the ListView window width.

I am a bit stuck, because I find no MouseOver event for the ListView elements. I would probably have to go on with a custom Style for my ListView, but I don't have experience with Styles.

I would really appreciate a little help, to get me started!

2

2 Answers

0
votes

Create an AttachedProperty for MouseMove and hook your list view with the property. The attached property can be used to any element in your application.

Attached Property

public class MouseBehaviour
{
 public static readonly DependencyProperty MouseMoveCommandProperty =
        DependencyProperty.RegisterAttached("MouseMoveCommand", typeof(ICommand), typeof(MouseBehaviour), new FrameworkPropertyMetadata(new PropertyChangedCallback(MouseMoveCommandChanged)));

    private static void MouseMoveCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        FrameworkElement element = (FrameworkElement)d;

        element.MouseMove += new MouseEventHandler(element_MouseMove);
    }

    static void element_MouseMove(object sender, MouseEventArgs e)
    {
        FrameworkElement element = (FrameworkElement)sender;

        ICommand command = GetMouseMoveCommand(element);

        command.Execute(e);
    }

    public static void SetMouseMoveCommand(UIElement element, ICommand value)
    {
        element.SetValue(MouseMoveCommandProperty, value);
    }

    public static ICommand GetMouseMoveCommand(UIElement element)
    {
        return (ICommand)element.GetValue(MouseMoveCommandProperty);
    }
}

XAML

xmlns:mousebehav="clr-namespace:your namespace"

<ListView mousebehav:MouseBehaviour.MouseMoveCommand="{Binding MouseMoveCommand}">

VM

private RelayCommand _MouseMoveCommand;
    public RelayCommand MouseMoveCommand
    {
        get
        {
            if (_MouseMoveCommand== null) return _MouseMoveCommand= new RelayCommand(param => Execute_MouseMoveCommand((MouseEventArgs)param));
            return _MouseMoveCommand;
        }
        set { _MouseMoveCommand= value; }
    }

private void Execute_MouseMoveCommand(MouseEventArgs param)
    {
        //your logic to expand or ??
    }
0
votes

Thanks for the answer. After a few hours of experimenting, I managed to solve it quite compact from the xaml:

  <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
             <Setter Property="ToolTip" Value="{Binding Name}" />
        </Style>
  </ListView.ItemContainerStyle>