當前位置: 首頁>>代碼示例>>C#>>正文


C# TextBlock.AssignFont方法代碼示例

本文整理匯總了C#中System.Windows.Controls.TextBlock.AssignFont方法的典型用法代碼示例。如果您正苦於以下問題:C# TextBlock.AssignFont方法的具體用法?C# TextBlock.AssignFont怎麽用?C# TextBlock.AssignFont使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Windows.Controls.TextBlock的用法示例。


在下文中一共展示了TextBlock.AssignFont方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: FormatData

        public Table FormatData(DataView data, IEnumerable<PrintColumnInfo> columnsInfo, double width)
        {
            var contentTable = new Table();

            //find the longes columns values from the data
            Dictionary<PrintColumnInfo, string> columnsMaxValues = GetCellMaxLengthRow(data, columnsInfo);
            //Compute columns width in UI Grid first and than use them in FlowDocument Table
            /* Unfortunately Table element from FlowDocument does not support auto column widthes
             * and Grid elemend could not be printed on multiple pages.
             * That's why I used "doble rendering" trick. First render text in UI and store columns
             * width and then use determined columns width in FlowDocument Table
             */
            //Creating fake grid to determing columns width
            var fakeGrid = new Grid();
            // creting real data width on print page
            fakeGrid.Width = width;
            //setting width of the UI Grid columns
            foreach (PrintColumnInfo colInfo in columnsInfo)
            {
                var colDef = new ColumnDefinition();
                colDef.Width = PrintLengthToGridLengthConverter.Convert(colInfo.ColumnWidth);
                fakeGrid.ColumnDefinitions.Add(colDef);
            }
            // adding header row and content row to the grid
            // as content width could be smaller than header width
            var headerRow = new RowDefinition();
            headerRow.Height = GridLength.Auto;
            fakeGrid.RowDefinitions.Add(headerRow);
            var contentRow = new RowDefinition();
            contentRow.Height = GridLength.Auto;
            fakeGrid.RowDefinitions.Add(contentRow);

            //adding content and header to the UI grid
            int counter = 0;
            foreach (var columnInfo in columnsMaxValues)
            {
                //add header
                var tbHeader = new TextBlock();
                tbHeader.Padding = new Thickness(2);
                tbHeader.AssignFont(HeaderFont);
                tbHeader.Text = columnInfo.Key.ColumnHeader;
                fakeGrid.Children.Add(tbHeader);
                Grid.SetColumn(tbHeader, counter);
                Grid.SetRow(tbHeader, 0);
                //add data
                var tbContent = new TextBlock();
                tbContent.Padding = new Thickness(2);
                tbContent.AssignFont(TableCellFont);
                tbContent.Text = columnInfo.Value;
                fakeGrid.Children.Add(tbContent);
                Grid.SetColumn(tbContent, counter);
                Grid.SetRow(tbContent, 1);

                counter++;
            }

            // Fake rendering. It causes grid to calculate its children size
            // without doing actual rendering
            fakeGrid.Measure(new Size(width, double.MaxValue));
            fakeGrid.Arrange(new Rect(new Size(width, double.MaxValue)));

            //getting columns width
            var columnWidthes = new double[columnsMaxValues.Count];
            for (int i = 0; i < columnWidthes.Length; i++)
            {
                columnWidthes[i] = fakeGrid.ColumnDefinitions[i].ActualWidth;
            }

            int columnCount = columnsInfo.Count();
            // adding column and setting their width
            for (int i = 0; i < columnCount; i++)
            {
                var tableColumn = new TableColumn();
                tableColumn.Width = new GridLength(columnWidthes[i]);
                contentTable.Columns.Add(tableColumn);
            }

            //adding headers for column
            contentTable.RowGroups.Add(new TableRowGroup());
            contentTable.RowGroups[0].Rows.Add(new TableRow());
            TableRow currentRow = contentTable.RowGroups[0].Rows[0];
            foreach (PrintColumnInfo column in columnsInfo)
            {
                currentRow.Cells.Add(new TableCell(new Paragraph(new Bold(new Run(column.ColumnHeader)))));
            }
            //adding Blue line
            contentTable.RowGroups[0].Rows.Add(new TableRow());
            currentRow = contentTable.RowGroups[0].Rows[1];
            var lineCell = new TableCell();
            lineCell.ColumnSpan = columnCount;
            lineCell.BorderThickness = new Thickness(0, 2, 0, 0);
            lineCell.BorderBrush = Brushes.Blue;
            currentRow.Cells.Add(lineCell);
            int colorCount = 0;

            // adding data to the table
            foreach (DataRowView dataRowView in data)
            {
                currentRow = new TableRow();
                currentRow.Background = ++colorCount%2 == 0 ? Brushes.LightGray : Brushes.White;
//.........這裏部分代碼省略.........
開發者ID:ivan-danilov,項目名稱:XpsPrinting,代碼行數:101,代碼來源:TableFormatter.cs


注:本文中的System.Windows.Controls.TextBlock.AssignFont方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。