WPF: как автоматически показывать мою подсказку ListView, когда фокус - PullRequest
0 голосов
/ 29 декабря 2018

Итак, у меня есть 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 голосов
/ 30 декабря 2018

В общем, это действительно плохая идея.У всплывающих подсказок есть очень специфическое поведение, поэтому 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...
...