Как я могу поместить пользовательский элемент управления в просмотрщик документов? - PullRequest
1 голос
/ 12 августа 2011

Можно ли поместить пользовательский элемент управления в просмотрщик doument? Если возможно, как это будет?

1 Ответ

4 голосов
/ 12 августа 2011

Вы можете использовать следующее ..

Редактировать
Добавлен Grid, который связывает его Width/Height с FixedPage ActualWidth/ActualHeight для достижения центрирования

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