FlowDocumentReader滚动到顶部

时间:2013-08-28 15:04:18

标签: .net wpf flowdocument flowdocumentreader

如何滚动到FlowDocumentReader的顶部?

内容是通过绑定设置的

<FlowDocumentReader Grid.Row="4" Grid.Column="0" Name="FlowDocumentPageViewer1" HorizontalAlignment="Stretch">
    <FlowDocumentReader.Document>
        <Binding ElementName="_this" Path="DocFlow" IsAsync="False" Mode="OneWay"/>
    </FlowDocumentReader.Document>                   
</FlowDocumentReader>

如果我向下滚动然后绑定新内容,它就不会滚动到顶部 有了新内容,我想滚动到顶部。

根据Clemnes的评论,这将滚动到顶部

FlowDocumentPageViewer1.Document.BringIntoView();

现在我的问题是如何自动化这个电话 我不能把它放在get中,因为在返回后无法输入该命令 试过这两个事件,但没有用绑定更新解雇

Loaded="FlowDocumentPageViewer1_loaded"  
SourceUpdated="FlowDocumentPageViewer1_loaded"

1 个答案:

答案 0 :(得分:1)

您可以创建一个附加属性来设置原始Document属性并调用BringIntoView()

public class FlowDocumentReaderEx
{
    public static readonly DependencyProperty DocumentProperty =
        DependencyProperty.RegisterAttached(
            "Document", typeof(FlowDocument), typeof(FlowDocumentReaderEx),
            new FrameworkPropertyMetadata(DocumentPropertyChanged));

    public static FlowDocument GetDocument(DependencyObject obj)
    {
        return (FlowDocument)obj.GetValue(DocumentProperty);
    }

    public static void SetDocument(DependencyObject obj, FlowDocument value)
    {
        obj.SetValue(DocumentProperty, value);
    }

    private static void DocumentPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        var flowDocumentReader = obj as FlowDocumentReader;

        if (flowDocumentReader != null)
        {
            flowDocumentReader.Document = e.NewValue as FlowDocument;

            if (flowDocumentReader.Document != null)
            {
                flowDocumentReader.Document.BringIntoView();
            }
        }
    }
}

现在您可以绑定此属性,如:

<FlowDocumentReader ...
    local:FlowDocumentReaderEx.Document="{Binding DocFlow, ElementName=_this}"/>