2011-08-12 71 views

回答

4

您可以使用以下..

編輯
新增結合其Width/HeightGridFixedPageActualWidth/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的設計師在這裏打破並且您會收到一條消息,指出「Property'網頁'不支持'PageContent'類型的值。
This在這裏報道:WPF FixedDocument object doesn't allow PageContent children

正如你可以在代碼中加載它背後

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

一個問題解決方法,我怎麼能將用戶控件集中在頁面中?因爲它出現在角落 –

+0

@ oscar.fimbres:請參閱我的更新回答 –

+0

@ oscar.fimbres:如果我的答案解決了您的問題,請將其標記爲已接受,以便其他人可以更容易地爲其提供幫助:) –