我想要一个表格,根据内容在逻辑上调整列的大小。这在 WPF 中可能吗?
替代文字 http://img43.imageshack.us/img43/2640/flowdocument.jpg
这是我正在使用的代码:
<Window x:Class="FlowDocument.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">
<Window.Resources>
<Style TargetType="{x:Type TableCell}">
<Setter Property="BorderBrush" Value="Gray" />
<Setter Property="BorderThickness" Value="3" />
</Style>
<Style TargetType="{x:Type Paragraph}">
<Setter Property="Padding" Value="2, 2, 2, 2" />
</Style>
</Window.Resources>
<Grid>
<FlowDocumentScrollViewer>
<FlowDocument>
<Table>
<Table.Columns>
<TableColumn Background="LightBlue" />
<TableColumn Background="Coral" />
</Table.Columns>
<TableRowGroup>
<TableRow>
<TableCell>
<Paragraph>This is a long piece of text</Paragraph>
</TableCell>
<TableCell>
<Paragraph>This isn't</Paragraph>
</TableCell>
</TableRow>
<TableRow>
<TableCell>
<Paragraph>This is a another long piece of text. The column should be …
Run Code Online (Sandbox Code Playgroud) 2022 年 12 月 13 日,微软为 .NET 4.8.1 发布了 Windows 更新 KB5020880 (CVE-2022-41089),应该可以修复 XPS 上的安全问题。
从那时起,将渲染到 XPS 的 FlowDocument 将不再显示本地图像。我确实需要本地图像,因为我必须动态创建和嵌入它们。
<FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
PageHeight="29.7cm"
PageWidth="21cm" >
<Section Padding="40,0,20,0">
<Paragraph>before image</Paragraph>
<Paragraph FontSize="10" FontFamily="Verdana">
<Image Source="c:/Test/MyImage.jpg" Margin="40,40,0,0" />
</Paragraph>
<Paragraph>after image</Paragraph>
</Section>
</FlowDocument>
Run Code Online (Sandbox Code Playgroud)
一旦 FlowDocument 转换为 XpsDocument,DocumentViewer 就会像这样显示它。
有人有解决方案吗?
非常欢迎任何帮助。
干杯,爵士乐
我有一个像这样开始的Xaml文件:
<FlowDocument
x:Name="flowDocument"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Drawing="clr-namespace:System.Drawing;assembly=System.Drawing"
Run Code Online (Sandbox Code Playgroud)
当前解决方案通过使用flowdocument引用xaml文件的物理路径来使用StremReader,然后将数据解析到模板中.
这不是一个有效的解决方案,因此我需要在没有引用物理路径的情况下获取flowdocument.
我想在我的C#代码中使用xmlns命名空间或类似命令
string result = XamlWriter.Save(flowDocument)
Run Code Online (Sandbox Code Playgroud)
并使用结果进行解析.
建议?
由于BlockUIContainer中的ItemsControl,我有一个高度不同的FlowDocument.在某些情况下,ItemsControl超出了页面高度.如果需要,有没有办法在打印前缩放FlowDocument以适合页面(8.5"X 11")?
截至目前,FlowDocument被命名为'doc',我正在使用的打印方法是:
private void Print_Click(object sender, RoutedEventArgs e)
{
PrintDialog pd = new PrintDialog();
doc.PageHeight = pd.PrintableAreaHeight;
doc.PageWidth = pd.PrintableAreaWidth;
doc.ColumnGap = 0;
doc.ColumnWidth = pd.PrintableAreaWidth;
IDocumentPaginatorSource dps = doc;
pd.PrintDocument(dps.DocumentPaginator, "Sheet");
}
Run Code Online (Sandbox Code Playgroud) 我在WPF中有一个如下所示的flowdocument:
<FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Paragraph FontFamily="Georgia">
<StackPanel>
<TextBlock Text="{Binding Path=Title}"/>
<TextBlock Text="{Binding Path=AssignedTo}"/>
</StackPanel>
</Paragraph>
</FlowDocument>
Run Code Online (Sandbox Code Playgroud)
而不是给DataContext一个带有Title和AssignedTo属性的类,我想给它一个该类的List并让flowdocument显示它们中的每一个对象.有人能告诉我如何在flowdocument中形成XAML来做到这一点吗?
我移植从应用WPF来的Silverlight和悲痛地读取的缺少的FlowDocument的支持.
Silverlight然后用标记显示文本的最佳方法是什么?
我只需要基础知识,例如
我不是指RichTextBox(在Vectorlight演示中),而是一种在应用程序表面上格式化文本的方法,就像我在WPF中使用FlowDocument一样:
有一个在我们的WinForms托管WPF应用程序内存泄漏FlowDocumentReader
的一个ElementHost
.我在一个简单的项目中重新创建了这个问题,并添加了下面的代码.
当我按下button1
:
UserControl1
只包含a的A FlowDocumentReader
被创建并设置为ElementHost
'sChild
FlowDocument
是从文本文件创建的(它只包含一个FlowDocument
带有StackPanel
几千行的文件<TextBox/>
)FlowDocumentReader
的Document
属性设置为这FlowDocument
此时,页面呈现FlowDocument
正确.正如预期的那样,使用了大量内存.
如果button1
再次单击,则内存使用量会增加,并且每次重复该过程时都会不断增加!尽管使用了大量新内存,GC仍未收集!没有参考不应该存在,因为:
如果我按下button2
哪个设置elementHost1.Child
为null并调用GC(参见下面的代码),则会发生另一个奇怪的事情 - 它不会清理内存,但如果我继续点击它几秒钟,它最终将释放它!
我们所有这些记忆都被使用是不可接受的.此外,ElementHost
从Controls
集合中删除Disposing
它,将引用设置为null,然后调用GC不会释放内存.
button1
点击多次,内存使用量不应该继续增加这不是内存使用无关紧要的事情,我可以随时让GC收集它.它实际上最终明显减慢了机器的速度.
如果你只是想下载VS项目,我已经在这里上传了它:http: //speedy.sh/8T5P2/WindowsFormsApplication7.zip
否则,这是相关代码.只需在设计器中为表单添加2个按钮,然后将它们连接到事件即可.Form1.cs中:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using …
Run Code Online (Sandbox Code Playgroud) 我有:
DocumentPaginator dp = ((IDocumentPaginatorSource)document).DocumentPaginator;
Run Code Online (Sandbox Code Playgroud)
和:
FlowDocument document = new FlowDocument();
// create a flow document...
Run Code Online (Sandbox Code Playgroud)
如何使用DocumentPaginator将FlowDocument转换为FixedDocument?我似乎无法找到如何做到这一点.我查看的所有网站都使用XpsWriter,如:
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
DocumentPaginator dp = ((IDocumentPaginatorSource)document).DocumentPaginator;
writer.Write(dp);
Run Code Online (Sandbox Code Playgroud)
我可以编写XPS然后返回FixedDocument但是必须有更好的方法吗?
我想FlowDocument
根据搜索结果突出显示文本的某些部分.我正在做的是获取搜索词在文本中出现的FlowDocument
索引,然后在找到的索引处开始的文本范围上应用背景颜色,结束于找到的索引+搜索词长度.
TextRange content = new TextRange(myFlowDocument.ContentStart,
myFlowDocument.ContentEnd);
List<int> highlights = GetHighlights(content.Text, search);
foreach (int index in highlights)
{
var start = myFlowDocument.ContentStart;
var startPos = start.GetPositionAtOffset(index);
var endPos = start.GetPositionAtOffset(index + search.Length);
var textRange = new TextRange(startPos, endPos);
textRange.ApplyPropertyValue(TextElement.BackgroundProperty,
new SolidColorBrush(Colors.Yellow));
}
TextRange newRange = new TextRange(myFlowDocument.ContentStart,
newDocument.ContentEnd);
FlowDocument fd = (FlowDocument)XamlReader.Parse(newRange.Text);
Run Code Online (Sandbox Code Playgroud)
问题是,我正在搜索文档文本中的索引,但是当我返回时,FlowDocument
添加了xaml标记,并且我看到了突出显示的内容.我该如何解决?
我将FlowDocument与BlockUIContainer和InlineUIContainer元素一起使用,这些元素包含(或作为基类)一些自定义块-SVG,数学公式等。因此,使用Selection.Load(stream,DataFormats.XamlPackage)不能正常工作,因为序列化将删除*的内容UIContainers,除非Child属性是Microsoft参考源中提供的图像:
private static void WriteStartXamlElement(...)
{
...
if ((inlineUIContainer == null || !(inlineUIContainer.Child is Image)) &&
(blockUIContainer == null || !(blockUIContainer.Child is Image)))
{
...
elementTypeStandardized = TextSchema.GetStandardElementType(elementType, /*reduceElement:*/true);
}
...
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,唯一的选择是使用可以完美运行的XamlWriter.Save和XamlReader.Load,序列化和反序列化FlowDocument的所有必需属性和对象,但必须手动实现Copy + Paste作为Copy +的默认实现粘贴使用Selection.Load / Save。
复制/粘贴非常重要,因为它还用于处理RichTextBox控件中或控件之间的元素拖动-无需自定义拖动代码即可操作对象的唯一方法。
这就是为什么我要使用FlowDocument序列化实现复制/粘贴,但是不幸的是,它存在一些问题:
显然,无法将对象从一个文档中删除并添加到另一个文档中(我最近发现了一个死胡同):'InlineCollection'元素无法插入树中,因为它已经是树的子级了。
[TextElementCollection.cs]
public void InsertAfter(TextElementType previousSibling, TextElementType newItem)
{
...
if (previousSibling.Parent != this.Parent)
throw new InvalidOperationException(System.Windows.SR.Get("TextElementCollection_PreviousSiblingDoesNotBelongToThisCollection", new object[1]
{
(object) previousSibling.GetType().Name
}));
...
}
Run Code Online (Sandbox Code Playgroud)
我想考虑在所有需要移到另一个文档的元素中使用反射来设置FrameworkContentElement._parent,但这是不得已而又肮脏的解决方案:
从理论上讲,我只能复制所需的对象:(可选)在选择的开头部分运行带有文本的文本,在and之间的所有段落和内联以及(可能)在结尾部分运行的部分,将它们封装在自定义类中,并使用进行序列化/反序列化XamlReader / XamlWriter。
这是带有部分工作的自定义复制/粘贴代码的自定义RichTextBox控件实现:
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents; …
Run Code Online (Sandbox Code Playgroud) flowdocument ×10
wpf ×7
c# ×4
.net ×2
elementhost ×1
highlight ×1
hyperlink ×1
itemscontrol ×1
markup ×1
scale ×1
silverlight ×1
winforms ×1
xaml ×1
xamlreader ×1
xamlwriter ×1
xps ×1