WPF:焦点时如何自动显示我的ListView工具提示

时间:2018-12-29 17:49:52

标签: c# wpf tooltip listviewitem

所以我的对象中有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;
}

还有ToolTip,我想当我的ListViewItem IsSelected=True出示我的ToolTip

这是我的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>

当我用ListViewItem箭头移过Up & Down时,我想自动显示TooTip,而不仅是在MouseIsOver时显示

我还尝试在我的ListViewItem style中使用它:

    <MultiDataTrigger>
        <MultiDataTrigger.Conditions>
            <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="False" />
            <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="True"/>
        </MultiDataTrigger.Conditions>
        <Setter Property="Foreground" Value="Black"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderBrush" Value="Silver"/>
        <Setter Property="BorderThickness" Value="0.5"/>
        <Setter Property="ToolTip" Value="{Binding Path=Text}"/>
    </MultiDataTrigger>

它们都不起作用,只有在ToolTip时我才能看到我的MouseIsOver

1 个答案:

答案 0 :(得分:1)

通常,这是一个非常糟糕的主意。工具提示具有非常特定的行为,这就是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;
        }
    }
}

然后您将这样使用:

                <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...