【问题标题】:wpf ListView item expand at mouseoverwpf ListView 项目在鼠标悬停时展开
【发布时间】:2015-08-05 13:46:59
【问题描述】:

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

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

非常感谢您的帮助,让我开始!

【问题讨论】:

    标签: wpf listview


    【解决方案1】:

    为 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}">
    

    虚拟机

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-10-20
        • 2012-11-29
        • 1970-01-01
        • 1970-01-01
        • 2011-06-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多