wpf ListView项目在鼠标悬停时展开

时间:2015-08-05 13:47:00

标签: wpf listview

我有一个ListView,有时听起来很长。我想创建一个事件,如果我将鼠标拖到一个元素上,整个名称将出现在一个类似于工具提示的小窗口中,其中包含该项目的整个文本。这样,即使ListView窗口宽度太长,用户也可以读取它。

我有点卡住了,因为我找不到ListView元素的MouseOver事件。我可能不得不为我的ListView继续使用自定义样式,但我没有使用样式的经验。

我真的很感激一点帮助,让我开始吧!

2 个答案:

答案 0 :(得分:0)

为MouseMove创建AttachedProperty并使用属性挂钩列表视图。附加属性可用于应用程序中的任何元素。

  

附属物

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 ??
    }

答案 1 :(得分:0)

感谢您的回答。经过几个小时的实验,我设法从xaml解决它非常紧凑:

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