2009-11-04 37 views
8

Tôi đã tạo ảnh này trong MS Word và tôi đang cố gắng sao chép kiểu trong ứng dụng WPF của mình bằng Tài liệu. Đầu tiên là 'từ':Tài liệu WPF: Nhận đường viền của ô Bảng phải

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

Tiếp nỗ lực của tôi để tái tạo:

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

Câu hỏi của tôi có lẽ là khá rõ ràng. Tôi đang làm gì sai? Tôi không thể tìm thấy thuộc tính padding trên hàng nhóm hoặc hàng. Dưới đây là mã của tôi:

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

Trả lời

9

Đáng tiếc là bạn không thể thiết lập biên giới cho một TableRow trong một FlowDocument. Nó chỉ có sẵn cho Table hoặc TableCell. Ngay cả tôi tự hỏi tại sao điều này không được cung cấp.

Mặc dù một trong những cách để đạt được một hiệu ứng biên giới hàng là sử dụng biên giới của tất cả các tế bào kết hợp với BorderThickness, và thiết lập CellSpacing của container Table để 0. Đối với ví dụ:

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

Yogesh, xin lỗi cho điều này câu trả lời muộn nhưng tôi mới đến câu hỏi này. Có lẽ câu trả lời có thể giúp người khác.

Trong trường hợp cụ thể này, bạn cần đặt bảng.BorderThickness thành 1, table.CellSpacing thành 0 và đường viền trên cùng hoặc dưới cùng cho mỗi ô.

Để tránh đặt độ dày thành (0,1,0,0) cho mỗi ô, bạn có thể sử dụng kiểu. Có rất nhiều cách để làm điều này, nhưng tôi sẽ chỉ cho bạn một cách đơn giản. Trong App.xaml của bạn, viết như sau:

<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> 

Sau đó, kết hợp các từ điển ứng dụng vào tài liệu hoặc bảng của bạn, với một cái gì đó như:

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

Bạn có thể có phong cách cho toàn bộ tài liệu, một bảng riêng lẻ và thậm chí là một hàng hoặc một ô riêng lẻ.

+0

nếu các ô không có cùng chiều cao thì bạn gặp khó khăn – GorillaApe

Các vấn đề liên quan