Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF:如何在聚焦时自动显示我的ListView工具提示_C#_Wpf_Tooltip_Listviewitem - Fatal编程技术网

C# WPF:如何在聚焦时自动显示我的ListView工具提示

C# WPF:如何在聚焦时自动显示我的ListView工具提示,c#,wpf,tooltip,listviewitem,C#,Wpf,Tooltip,Listviewitem,因此,我的对象有ListViewItem: public class ClipboardItem : INotifyPropertyChanged { private string _text { get; set; } public string Text { get { return _text; } set { _text = value; NotifyPropertyC

因此,我的对象有
ListViewItem

public class ClipboardItem : INotifyPropertyChanged
{
    private string _text { get; set; }
    public string Text
    {
        get { return _text; }
        set
        {
            _text = value;
            NotifyPropertyChanged();
        }
    }

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}
工具提示
,我希望当我的
列表视图项
IsSelected=True
时显示我的
工具提示

这是我的
ListViewItem
CellTemplate

                                                <TextBlock Text="{Binding Path=Text}"
                                                           Foreground="{Binding Path=Foreground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListViewItem}}}"
                                                           Grid.Column="1"
                                                           Margin="5,0,0,0">
                                                    <TextBlock.ToolTip>
                                                        <ToolTip Content="{Binding Path=Text}"
                                                                 Placement="Left" 
                                                                 PlacementRectangle="0,0,0,0"
                                                                 HorizontalOffset="10" 
                                                                 VerticalOffset="20"
                                                                 HasDropShadow="false"/>
                                                    </TextBlock.ToolTip>
                                                </TextBlock>

它们都不起作用,我只能在鼠标悬停时才能看到工具提示。一般来说,这是一个非常糟糕的主意。工具提示有一个非常特殊的行为,这就是为什么WPF没有像WinForms在
ToolTip.Show中那样方便地公开它的原因。这里正确使用的东西应该是装饰物

这就是说,如果您绝对坚持手动强制显示工具提示,那么可以通过行为来完成,但您将不得不放弃一些通常为您提供的功能:

using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;

namespace YourApp.Behaviors
{
    public class ToolTipBehavior : Behavior<FrameworkElement>
    {
        private ToolTip CurrentToolTip;

        public ListViewItem ListViewItem
        {
            get { return (ListViewItem)GetValue(ListViewItemProperty); }
            set { SetValue(ListViewItemProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ListViewItem.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ListViewItemProperty =
            DependencyProperty.Register("ListViewItem", typeof(ListViewItem), typeof(ToolTipBehavior),
                new PropertyMetadata(null, (d, e) => (d as ToolTipBehavior)?.OnListViewItemChanged(e)));

        private void OnListViewItemChanged(DependencyPropertyChangedEventArgs e)
        {
            if (e.OldValue is ListViewItem)
                (e.OldValue as ListViewItem).Selected -= ToolTipBehavior_Selected;
            if (e.NewValue is ListViewItem)
                (e.NewValue as ListViewItem).Selected += ToolTipBehavior_Selected;
        }

        private void ToolTipBehavior_Selected(object sender, RoutedEventArgs e)
        {
            if (e.Source != e.OriginalSource)
                return;
            if ((this.ListViewItem != null) && this.ListViewItem.IsSelected)
            {
                var tooltip = this.AssociatedObject.ToolTip as ToolTip;
                if (tooltip != null)
                {
                    if (this.CurrentToolTip != tooltip)
                    {
                        if (this.CurrentToolTip != null)
                            this.CurrentToolTip.Opened -= Tooltip_Opened;
                        this.CurrentToolTip = tooltip;
                        if (this.CurrentToolTip != null)
                            this.CurrentToolTip.Opened += Tooltip_Opened;
                    }
                    this.CurrentToolTip.PlacementTarget = this.AssociatedObject;
                    this.CurrentToolTip.IsOpen = true;
                }
            }
        }

        private async void Tooltip_Opened(object sender, RoutedEventArgs e)
        {
            await Task.Delay(1000);
            (this.AssociatedObject.ToolTip as ToolTip).IsOpen = false;
        }
    }
}
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Interactive;
命名空间YourApp.Behaviors
{
公共类ToolTipBehavior:行为
{
私人工具提示当前工具提示;
公共ListViewItem ListViewItem
{
获取{return(ListViewItem)GetValue(ListViewItemProperty);}
set{SetValue(ListViewItemProperty,value);}
}
//使用DependencyProperty作为ListViewItem的备份存储。这将启用动画、样式设置、绑定等。。。
公共静态只读DependencyProperty ListViewItemProperty=
DependencyProperty.Register(“ListViewItem”、typeof(ListViewItem)、typeof(ToolTipBehavior),
新属性元数据(null,(d,e)=>(d作为ToolTipBehavior)?.OnListViewItemChanged(e));
私有void OnListViewItemChanged(DependencyPropertyChangedEventArgs e)
{
如果(例如,OldValue为ListViewItem)
(e.OldValue作为ListViewItem)。选中-=工具提示行为\u选中;
如果(例如,NewValue是ListViewItem)
(e.NewValue作为ListViewItem)。选中+=工具提示行为\u选中;
}
已选择专用无效工具提示行为(对象发送者、路由目标)
{
if(e.Source!=e.OriginalSource)
回来
if((this.ListViewItem!=null)&&this.ListViewItem.IsSelected)
{
var tooltip=this.AssociatedObject.tooltip作为工具提示;
如果(工具提示!=null)
{
if(this.CurrentToolTip!=工具提示)
{
if(this.CurrentToolTip!=null)
this.CurrentToolTip.Opened-=工具提示\u已打开;
this.CurrentToolTip=工具提示;
if(this.CurrentToolTip!=null)
this.CurrentToolTip.Opened+=工具提示\u Opened;
}
this.CurrentToolTip.PlacementTarget=this.AssociatedObject;
this.CurrentToolTip.IsOpen=true;
}
}
}
专用异步无效工具提示\u已打开(对象发送方,路由目标)
{
等待任务。延迟(1000);
(this.AssociatedObject.ToolTip作为工具提示)。IsOpen=false;
}
}
}
然后,您将使用如下方式:

                <TextBlock Text="{Binding Path=Text}"
                    Foreground="{Binding Path=Foreground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListViewItem}}}"
                    Grid.Column="1"
                    Margin="5,0,0,0">

                    <i:Interaction.Behaviors>
                        <behaviors:ToolTipBehavior ListViewItem="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListViewItem}}}" />
                    </i:Interaction.Behaviors>

                    <TextBlock.ToolTip>
                        <ToolTip Content="{Binding Path=Text}" ToolTipService.ShowDuration="2"

                        ...etc...


这是什么?互动?我添加了以下内容:xmlns:behaviors=“clr namespace:ClipboardManager.Classes.behaviors”,但我如何使用它呢?这是一种行为,您可以阅读有关它们的信息,并使用它们。要使用它们,您只需添加交互性名称空间:
xmlns:i=“clr namespace:System.Windows.interactivity;assembly=System.Windows.interactivity”
                <TextBlock Text="{Binding Path=Text}"
                    Foreground="{Binding Path=Foreground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListViewItem}}}"
                    Grid.Column="1"
                    Margin="5,0,0,0">

                    <i:Interaction.Behaviors>
                        <behaviors:ToolTipBehavior ListViewItem="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListViewItem}}}" />
                    </i:Interaction.Behaviors>

                    <TextBlock.ToolTip>
                        <ToolTip Content="{Binding Path=Text}" ToolTipService.ShowDuration="2"

                        ...etc...