将标签内容更改为WPF中当前悬停的ListView项 [英] Change label content to currently hovered ListView item in WPF
问题描述
一个有趣的问题..
让我们假设你有一个这样的列表视图:
< < 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屋!