将标签内容更改为WPF中当前悬停的ListView项 [英] Change label content to currently hovered ListView item in WPF

查看:78
本文介绍了将标签内容更改为WPF中当前悬停的ListView项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我到处寻找答案,但一无所获。我的窗口底部有一个StatusBar,里面有一个标签,右边是一个ListView。这些项目将在整个程序执行过程中发生变化。每当鼠标在ListView中的项目上被HOVERED时,我想将StatusBar中标签的内容更改为某些内容。就像在几个程序上,当您将鼠标悬停在菜单中的项目上时,您会看到动作在StatusBar中显示的内容的简短描述。我想要这个,但使用ListView。我不仅希望在标签中镜像项目的文本,我想为它提供我自己的文本。所以我只需要获取当前悬停的项目,然后我可以使用if语句在标签中显示消息。我不知道如何做到这一点(在ListView中获取悬停的项目)。谢谢。

解决方案

一个有趣的问题..





让我们假设你有一个这样的列表视图:



 <  < span class =code-leadattribute> ListView  >  
< ListView.Resources >
< 样式 TargetType = ListViewItem >
< span class =code-leadattribute>< Setter Property =ToolTipValue ={ Binding Content,RelativeSource = {RelativeSource Mode = Self} }/>
< / 风格 >
< / ListView.Resources >
< ListViewItem > 1 < / ListViewItem >
< ListViewItem > 2 < / ListViewItem >
< ListViewItem > 3 < / ListViewItem >

< / ListView >





您可以使用MouseMoveEvent检索它:(在MainWindow.xaml.cs中)

 AddHandler(ListViewItem.MouseMoveEvent, (RoutedEventHandler)((o,e)= >  
{
if (e.Source ListViewItem)
{
string tooltip =(e。 Source as ListViewItem).ToolTip as string ;
if (tooltip!= null
Title = tooltip;
}
Debug.WriteLine(e.Source);
}));





如果要隐藏工具提示,则必须定义另一个附加属性,例如:



  public   static   readonly  DependencyProperty ToolTipProperty = 
DependencyProperty.RegisterAttached( ToolTip typeof string ), typeof (MainWindow),
new PropertyMetadata( ));

public static void SetToolTip(DependencyObject obj, string value
{
obj。 SetValue(ToolTipProperty, value );
}

public static string GetToolTip(DependencyObject obj)
{
return string )obj.GetValue(ToolTipProperty);
}









问候

Joseph Leung


OK, I have looked everywhere for an answer to this but have found nothing. I have a StatusBar at the bottom of my window with a label inside it and a ListView on the right. The items will change throughout the execution of the program. Whenever the mouse is HOVERED over an item in the ListView I want to change the content of the label in the StatusBar to something. Like on a few programs when you hover over items in the menu you see a short description of what the action does appear in a StatusBar. I want to to this but with a ListView instead. I don't just want the text of the item to be mirrored in the label, I want to provide my own text for it. So I just need to get the currently hovered over item and then I can use if statements to display a message in the label. I have no idea how to do this (get the hovered item in the ListView). Thank you.

解决方案

An interesting question..


Lets say you have a list view like this:

<ListView>
    <ListView.Resources>
        <Style TargetType="ListViewItem">
            <Setter Property="ToolTip" Value="{Binding Content, RelativeSource={RelativeSource Mode=Self}}" />
        </Style>
    </ListView.Resources>
    <ListViewItem>1</ListViewItem>
    <ListViewItem>2</ListViewItem>
    <ListViewItem>3</ListViewItem>

</ListView>



You can retrieve it using MouseMoveEvent: (in MainWindow.xaml.cs)

AddHandler(ListViewItem.MouseMoveEvent, (RoutedEventHandler)((o, e) =>
              {
                  if (e.Source is ListViewItem)
                  {
                      string tooltip = (e.Source as ListViewItem).ToolTip as string;
                      if (tooltip != null)
                          Title = tooltip;
                  }
                  Debug.WriteLine(e.Source);
              }));



If you want to hide the tooltip, you have to define another attached property, e.g.

public static readonly DependencyProperty ToolTipProperty =
           DependencyProperty.RegisterAttached("ToolTip", typeof(string), typeof(MainWindow),
           new PropertyMetadata(""));

       public static void SetToolTip(DependencyObject obj, string value)
       {
           obj.SetValue(ToolTipProperty, value);
       }

       public static string GetToolTip(DependencyObject obj)
       {
           return (string)obj.GetValue(ToolTipProperty);
       }





Regards
Joseph Leung


这篇关于将标签内容更改为WPF中当前悬停的ListView项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆