Tooltip on scrollviewer in documentviewer

2019-06-02 10:36发布

问题:

I have a documentviewer which i used in my wpf project to show xps document reports of having around 600 pages which is working great. But from user point of view i like to show the current page number as a tooltip on my scrollviewer while dragging the scroll stating the current page number in view. Somewhat like in a PDF file like this -

I was looking out for some ideas how to implement this. Just a current page number if not possible to show a thumbnail image would be good enough for me. Is there any in-built support in documentviewer for this functionality??

Thanks for any help..

回答1:

I cannot find anything like IsScrolling so i would approach it like this:

<Popup Name="docPopup" AllowsTransparency="True" PlacementTarget="{x:Reference docViewer}" Placement="Center">
    <Border Background="Black" CornerRadius="5" Padding="10" BorderBrush="White" BorderThickness="1">
        <TextBlock Foreground="White">
                    <Run Text="{Binding ElementName=docViewer, Path=MasterPageNumber, Mode=OneWay}"/>
                    <Run Text=" / "/>
                    <Run Text="{Binding ElementName=docViewer, Path=PageCount, Mode=OneWay}"/>
        </TextBlock>
    </Border>
</Popup>
<DocumentViewer Name="docViewer" ScrollViewer.ScrollChanged="docViewer_ScrollChanged"/>

The popup should be displayed when the document is scrolled, then it should fade out after some time. This is done in the handler:

DoubleAnimationUsingKeyFrames anim;
private void docViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
    if (anim == null)
    {
        anim = new DoubleAnimationUsingKeyFrames();
        anim.Duration = (Duration)TimeSpan.FromSeconds(1);
        anim.KeyFrames.Add(new DiscreteDoubleKeyFrame(1, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0))));
        anim.KeyFrames.Add(new DiscreteDoubleKeyFrame(1, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0.5))));
        anim.KeyFrames.Add(new LinearDoubleKeyFrame(0, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1))));
    }

    anim.Completed -= anim_Completed;
    docPopup.Child.BeginAnimation(UIElement.OpacityProperty, null);
    docPopup.Child.Opacity = 1;

    docPopup.IsOpen = true;

    anim.Completed += anim_Completed;
    docPopup.Child.BeginAnimation(UIElement.OpacityProperty, anim);
}

void anim_Completed(object sender, EventArgs e)
{
    docPopup.IsOpen = false;
}

Edit: The event fires also on scrolls done via mouse-wheel etc. you could wrap everything in the handler in if (Mouse.LeftButton == MouseButtonState.Pressed), not 100% accurate but who scrolls with the MouseWheel while left-clicking?