我怎样才能把一个文档查看器内的用户控件? [英] How can I put an user control inside a document viewer?

查看:142
本文介绍了我怎样才能把一个文档查看器内的用户控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能把doument浏览器内的用户控件?如果可能的话,怎么会是?

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

推荐答案

您可以使用以下..

修改结果
增加了一个电网结合了宽度/高度固定页面 ActualWidth的/的ActualHeight 来实现中心

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>



不幸的是,Visual Studio 2010的设计师在这里打破,你会得到一个消息,说财产的页面'不支持类型的值'PageContent`结果
该报告如下:的 WPF固定文档对象不允许pageContent孩子

作为一种变通方法,你可以加载它背后

As a workaround you can load it in code behind

的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);
}

这篇关于我怎样才能把一个文档查看器内的用户控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆