我在RichTextBox中有一些文本.此文本包含标签,例如:[@TagName!].我想用数据库中的一些数据替换这些标签而不会丢失格式(字体,颜色,图像等).我创建了一个方法:
void ReplaceTagsWithData(FlowDocument doc)
{
FileStream fs = new FileStream("tmp.xml", FileMode.Create);
TextRange trTextRange =
new TextRange(doc.ContentStart, doc.ContentEnd);
trTextRange.Save(fs, DataFormats.Xaml);
fs.Dispose();
fs.Close();
StreamReader sr = new StreamReader("tmp.xml");
string rtbContent = sr.ReadToEnd();
MatchCollection mColl =
Regex.Matches(rtbContent,
string.Format(@"\{0}+[a-zA-Z]+{1}",
prefix,
postfix));
foreach (Match m in mColl)
{
string colname =
m.Value.Substring(prefix.Length,
(int)(m.Value.Length - (prefix.Length + postfix.Length)));
rtbContent = rtbContent.Replace(m.Value.ToString(),
dt.Rows[0][colname].ToString());
}
rtbEdit.Document =
new FlowDocument(
(Section)XamlReader.Load(
XmlReader.Create(new StringReader(rtbContent))));
sr.Dispose();
sr.Close();
}
Run Code Online (Sandbox Code Playgroud)
它非常好,但它从内容中删除图像.我知道我应该使用XamlPackage而不是Xaml但是我不能把它作为纯文本.还有其他解决方案吗?
谢谢你的回答.;)
[编辑:2012年2月13日02:14(上午)]
我的工作方案:
void ReplaceTagsWithData(RichTextBox rtb)
{
FlowDocument doc = rtb.Document;
FileStream …
Run Code Online (Sandbox Code Playgroud) 我正在使用代码隐藏在WPF FlowDocument中呈现一个表.但是,我一直无法找到一个示例,说明如何使表只使用基于内容所需的空间.相反,该表占用了我不想要的所有可用宽度,我也不想指定精确的像素大小.
我显然遗漏了一些简单的东西,有人看到了吗?
var fd = new FlowDocument();
Table t = new Table();
t.BorderBrush = Brushes.Black;
t.BorderThickness = new Thickness(2);
// I thought this would do what I wanted...
t.Columns.Add(new TableColumn() { Width = GridLength.Auto });
t.Columns.Add(new TableCOlumn() { Width = GridLength.Auto });
TableRowGroup trg = new TableRowGroup();
TableRow currentRow = new TableRow();
currentRow.Cells.Add(new TableCell(new Paragraph(new Run("ABC"))));
currentRow.Cells.Add(new TableCell(new Paragraph(new Run("XYZ"))));
trg.Rows.Add(currentRow);
currentRow = new TableRow();
currentRow.Cells.Add(new TableCell(new Paragraph(new Run("123"))));
currentRow.Cells.Add(new TableCell(new Paragraph(new Run("789"))));
trg.Rows.Add(currentRow); …
Run Code Online (Sandbox Code Playgroud) 在WPF中,是否有一种简单的方法可以向FlowDocument
元素添加波浪下划线(如Word中的拼写错误)?这是Underline
班级,但似乎没有办法来设计它.
我的各种WPF应用程序显示FlowDocument的.我可以使用打印WPF FlowDocument 的答案中描述的方法打印它们.
现在我想添加一个"打印预览"功能.在正常情况下,我正在打印窗口中显示的FlowDocument,因此我不需要打印预览.但在某些情况下,要打印的FlowDocument是在内存中即时构建的.在这些情况下,我想在打印前显示它.
现在,我当然可以弹出一个新窗口并显示FlowDocument,但是
我希望预览真的感觉它是打印操作的一部分,而不仅仅是应用程序中的另一个窗口.
我不想在FlowDocumentScrollViewer中使用普通的FlowDocument.它不是"任何大小",而是需要约束纸张的大小,特定的HxW比率和分页.
建议?
我应该只使用标准窗口,在这种情况下,如何确保FlowDocument处于适当的比例?
是否有更"集成"的方式在Windows的PrintDialog UI范围内进行预览?
谢谢
我的WPF应用程序中有一个ListView,它绑定到要执行的任务集合(待办事项列表).我希望用户能够打印他们的列表,并根据MSDN指南创建以下代码.(这是我第一次涉足印刷业)
public FlowDocument GetPrintDocument()
{
FlowDocument flowDoc = new FlowDocument();
Table table = new Table();
int numColumns = 3;
flowDoc.Blocks.Add(table);
for(int x=0;x<numColumns;x++)
{
table.Columns.Add(new TableColumn());
}
GridLengthConverter glc = new GridLengthConverter();
table.Columns[0].Width = (GridLength)glc.ConvertFromString("300");
table.Columns[1].Width = (GridLength)glc.ConvertFromString("50");
table.Columns[2].Width = (GridLength)glc.ConvertFromString("50");
table.RowGroups.Add(new TableRowGroup());
table.RowGroups[0].Rows.Add(new TableRow());
// store current working row for reference
TableRow currentRow = table.RowGroups[0].Rows[0];
currentRow.FontSize = 16;
currentRow.FontWeight = FontWeights.Bold;
currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Subject"))));
currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Due Date"))));
currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Urgency"))));
for (int i = 1; i …
Run Code Online (Sandbox Code Playgroud) 我只想知道如何打印流文档而不在WPF中显示Print Dialog.
感谢帮助…
有没有办法使用数据绑定来显示或隐藏FlowDocument中的段落?(我想使用MVVM,但使用FlowDocument作为我的视图.)
段落没有Visibility属性.我不确定还有什么可以寻找的.
WPF应用程序中的以下代码创建一个超链接,其外观和行为类似于超链接,但在单击时不执行任何操作.
我需要更改什么,以便当我单击它时,它会打开默认浏览器并转到指定的URL?
替代文字http://www.deviantsart.com/upload/4fbnq2.png
XAML:
<Window x:Class="TestLink238492.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel Margin="10">
<ContentControl x:Name="MainArea"/>
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
代码背后:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
namespace TestLink238492
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
FlowDocumentScrollViewer fdsv = new FlowDocumentScrollViewer();
FlowDocument doc = new FlowDocument();
fdsv.Document = doc;
fdsv.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
doc.PagePadding = new Thickness(0);
Paragraph paragraph = new Paragraph();
doc.Blocks.Add(paragraph);
Run run = new Run("this is flow document …
Run Code Online (Sandbox Code Playgroud) 检测WPF RichTextBox/FlowDocument是否为空的最佳方法是什么?
如果文档中仅存在文本,则以下内容有效.如果它包含UIElement,则不是
new TextRange(Document.ContentStart, Document.ContentEnd).IsEmpty
Run Code Online (Sandbox Code Playgroud) TableCell
关于WPF 的分裂策略,我有一个问题FlowDocument
Table
.
这是一个允许重现问题的简单代码:
MainWindow.xaml.cs
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var table = new Table() { BorderThickness = new Thickness(1), BorderBrush = Brushes.Black, CellSpacing = 0 };
var rowGroup = new TableRowGroup();
var tableRow = new TableRow();
var cell1 = new TableCell() { Background = Brushes.Red, BorderThickness = new Thickness(0, 0, 1, 0), BorderBrush = Brushes.Black };
var cell2 = new TableCell() …
Run Code Online (Sandbox Code Playgroud) flowdocument ×10
wpf ×10
c# ×5
printing ×3
.net ×2
data-binding ×1
hyperlink ×1
richtextbox ×1
string ×1
underline ×1
visibility ×1