如何将用户控件放入文档查看器中?

发布于 2024-11-29 08:09:39 字数 37 浏览 1 评论 0原文

是否可以将用户控件放入文档查看器中?如果可以的话,会怎样呢?

Is it possible put an user control inside a doument viewer? If possible, how will it be that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是一片海 2024-12-06 08:09:40

您可以使用以下内容..

编辑
添加了一个将其宽度/高度绑定到固定页面的网格以实现居中

<DocumentViewer>
    <FixedDocument>
        <PageContent>
            <FixedPage HorizontalAlignment="Center">
                <Grid Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type FixedPage}},
                                      Path=ActualWidth}"
                      Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type FixedPage}},
                                       Path=ActualHeight}">
                    <local:MyUserControl HorizontalAlignment="Center"/>
                </Grid>
            </FixedPage>
        </PageContent>
    </FixedDocument>
</DocumentViewer>

不幸的是 Visual Studio 2010 设计器在这里被破坏,您将收到一条消息,指出“属性‘pages’不支持‘PageContent’类型的值。
此处报告:WPF FixedDocument对象不允许 PageContent 子元素

作为解决方法,您可以在

Xaml 后面的代码

<DocumentViewer>
    <FixedDocument Loaded="FixedDocument_Loaded"/>
</DocumentViewer>

中加载它代码后面

private void FixedDocument_Loaded(object sender, RoutedEventArgs e)
{
    FixedDocument fixedDocument = sender as FixedDocument;

    MyUserControl myUserControl = new MyUserControl();
    myUserControl.HorizontalAlignment = HorizontalAlignment.Center;
    myUserControl.VerticalAlignment = VerticalAlignment.Center;

    Grid grid = new Grid();            
    grid.Children.Add(myUserControl);

    FixedPage fixedPage = new FixedPage();
    fixedPage.Children.Add(grid);

    Binding widthBinding = new Binding("ActualWidth");
    widthBinding.Source = fixedPage;
    Binding heightBinding = new Binding("ActualHeight");
    heightBinding.Source = fixedPage;
    grid.SetBinding(Grid.WidthProperty, widthBinding);
    grid.SetBinding(Grid.HeightProperty, heightBinding);

    PageContent pageContent = new PageContent();
    (pageContent as IAddChild).AddChild(fixedPage);

    fixedDocument.Pages.Add(pageContent);
}

You can use the following..

Edit
Added a Grid which binds its Width/Height to the FixedPage ActualWidth/ActualHeight to achieve centering

<DocumentViewer>
    <FixedDocument>
        <PageContent>
            <FixedPage HorizontalAlignment="Center">
                <Grid Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type FixedPage}},
                                      Path=ActualWidth}"
                      Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type FixedPage}},
                                       Path=ActualHeight}">
                    <local:MyUserControl HorizontalAlignment="Center"/>
                </Grid>
            </FixedPage>
        </PageContent>
    </FixedDocument>
</DocumentViewer>

Unfortunately the Visual Studio 2010 designer is broken here and you'll get a message saying "Property 'pages' does not support values of type 'PageContent`.
This is reported here: WPF FixedDocument object doesn't allow PageContent children

As a workaround you can load it in code behind

Xaml

<DocumentViewer>
    <FixedDocument Loaded="FixedDocument_Loaded"/>
</DocumentViewer>

Code behind

private void FixedDocument_Loaded(object sender, RoutedEventArgs e)
{
    FixedDocument fixedDocument = sender as FixedDocument;

    MyUserControl myUserControl = new MyUserControl();
    myUserControl.HorizontalAlignment = HorizontalAlignment.Center;
    myUserControl.VerticalAlignment = VerticalAlignment.Center;

    Grid grid = new Grid();            
    grid.Children.Add(myUserControl);

    FixedPage fixedPage = new FixedPage();
    fixedPage.Children.Add(grid);

    Binding widthBinding = new Binding("ActualWidth");
    widthBinding.Source = fixedPage;
    Binding heightBinding = new Binding("ActualHeight");
    heightBinding.Source = fixedPage;
    grid.SetBinding(Grid.WidthProperty, widthBinding);
    grid.SetBinding(Grid.HeightProperty, heightBinding);

    PageContent pageContent = new PageContent();
    (pageContent as IAddChild).AddChild(fixedPage);

    fixedDocument.Pages.Add(pageContent);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文