2009-11-04 34 views
8

我在MS Word中創建了這張圖片,我試圖使用文檔在WPF應用程序中複製樣式。首先, '從':WPF文檔:獲取表格單元格的右邊框

alt text http://img337.imageshack.us/img337/1275/correntborder.png

接下來我試圖複製:

alt text http://img156.imageshack.us/img156/1711/extrawhiteborder.png

我的問題很可能是相當明顯的。我究竟做錯了什麼?我無法在行組或行上找到填充屬性。下面是我的代碼:

public override FlowDocument CreateDocumentSection(IInteractivityElement pElement) 
    { 
     var result = new FlowDocument(); 

     // show the header 
     result.Blocks.Add(CreateHeading(pElement.Header)); 

     // we don't show anything else if there aren't any columns 
     var nrColumns = pElement.GetIntegralData("CurrentColumnCount") ?? 0; 
     if (nrColumns == 0) return result; 

     Table mainTable = new Table(); 
     result.Blocks.Add(mainTable); 

     // columns 
     for (long tableIdx = 0; tableIdx < nrColumns; tableIdx++) 
     { 
      var newColumn = new TableColumn(); 
      mainTable.Columns.Add(newColumn); 
     } 

     // row group for header 
     TableRowGroup rowGroup = new TableRowGroup(); 
     mainTable.RowGroups.Add(rowGroup); 

     // row for header 
     TableRow headerRow = new TableRow(); 
     headerRow.Background = new SolidColorBrush(Color.FromRgb(79, 129, 189)); 
     headerRow.Foreground = new SolidColorBrush(Colors.White); 
     rowGroup.Rows.Add(headerRow); 

     // add columns for each header cell 
     for (long tableIdx = 0; tableIdx < nrColumns; tableIdx++) 
     { 
      var headerNameKey = CreateColumnNameKey(tableIdx); 
      TableCell headerCell = new TableCell(new Paragraph(new Run(pElement.GetStringData(headerNameKey)))); 
      headerRow.Cells.Add(headerCell); 
     } 

     TableRow emptyRow = new TableRow(); 
     emptyRow.Foreground = new SolidColorBrush(Colors.Gray); 
     rowGroup.Rows.Add(emptyRow); 

     TableCell emptyInstructionCell = new TableCell(); 
     emptyInstructionCell.BorderBrush = new SolidColorBrush(Color.FromRgb(79, 129, 189)); 
     emptyInstructionCell.BorderThickness = new Thickness(1.0); 
     emptyInstructionCell.ColumnSpan = Convert.ToInt32(nrColumns); 
     emptyInstructionCell.Blocks.Add(new Paragraph(new Run(pElement.Instruction))); 
     emptyRow.Cells.Add(emptyInstructionCell); 

     return result; 
    } 

回答

9

可惜你不能在FlowDocument設置邊框的TableRow。它僅適用於TableTableCell。即使我想知道爲什麼沒有提供。

雖然實現了連續邊框效果的一種方法是使用所有細胞的邊界連同BorderThickness,並設置容器TableCellSpacing爲0。例如:

table.CellSpacing = 0; 
... 
cellLeft.BorderThickness= new Thickness(1, 1, 0, 1); 
... 
cellCenter.BorderThickness= new Thickness(0, 1); 
... 
cellRight.BorderThickness= new Thickness(0, 1, 1, 1); 
5

約傑什,對不起,這遲到的答案,但我剛剛來到這個問題。也許答案可以幫助其他人。

在這種特殊情況下,您需要將table.BorderThickness設置爲1,將table.CellSpacing設置爲0,併爲每個單元格設置頂部或底部邊框。

爲了避免爲每個單元格設置厚度(0,1,0,0),可以使用樣式。有很多方法可以做到這一點,但我會告訴你一個簡單的方法。在你App.xaml中,寫上如下:

<Application x:Class="YourNamespace.App" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:doc="clr-namespace:System.Windows.Documents;assembly=PresentationFramework"> 

    <Application.Resources> 
     <Style TargetType="doc:TableCell" > 
      <Setter Property="BorderBrush" Value="Blue" /> 
      <Setter Property="BorderThickness" Value="0,1,0,0" /> 
      <Setter Property="FontSize" Value="12" /> 
      <Setter Property="Padding" Value="2" /> 
     </Style>   
    </Application.Resources> 
</Application> 

之後,合併應用字典到您的文檔或表格,喜歡的東西:

mainTable.Resources.MergedDictionaries.Add(App.Current.Resources); 

你可以有風格爲整個文檔,一張桌子,甚至一個單獨的行或單元格。

+0

如果單元格不在同一高度,那麼您有麻煩 – GorillaApe