Tooltip on scrollviewer in documentviewer

删除回忆录丶 提交于 2020-01-23 16:56:48

问题


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?



来源:https://stackoverflow.com/questions/5759074/tooltip-on-scrollviewer-in-documentviewer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!