所以我搞乱了固定的文件,我遇到了同样的问题。我认为这甚至可能比其他人建议的更干净。
所以基本上你应该创建一个从hillinFixedDocument
建议派生的自定义类,并添加一个属性以从该对象的. 但是由于这些页面现在是另一个对象的可视子对象,因此您应该使用 XmlReader 和 XmlWriter 类制作它们的副本。FixedDocument
PageContents
[ContentProperty("Pages")]
public class CustomFixedDocument : FixedDocument
{
private ObservableCollection<PageContent> _pages;
public CustomFixedDocument()
{
this.Pages = new ObservableCollection<PageContent>();
}
public FixedDocument FixedDocument
{
get
{
var document = new FixedDocument();
foreach (var p in Pages)
{
var copy = XamlReader.Parse(XamlWriter.Save(p)) as PageContent;
document.Pages.Add(copy);
}
return document;
}
}
public new ObservableCollection<PageContent> Pages
{
get => _pages;
set
{
_pages = value;
foreach (var page in _pages)
{
base.Pages.Add(page);
}
_pages.CollectionChanged += (o, e) =>
{
if (e.NewItems != null)
{
foreach (PageContent page in e.NewItems)
{
base.Pages.Add(page);
}
}
};
}
}
}
现在在 xaml 中,您可以轻松地创建一个CustomFixedDocument
StaticResource 并将您的绑定DocumentViewer
到它的“FixedDocument”属性。
<Window x:Class="MyProject.DocumentWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyProject"
mc:Ignorable="d"
Title="DocumentWindow" Height="450" Width="800" Loaded="Window_Loaded">
<Window.Resources>
<local:CustomFixedDocument x:Key="Report">
<PageContent>
<FixedPage Width="793.76" Height="1122.56">
<TextBlock Margin="50" Text="Page 1"/>
</FixedPage>
</PageContent>
<PageContent>
<FixedPage Width="793.76" Height="1122.56">
<TextBlock Margin="50" Text="Page 2"/>
</FixedPage>
</PageContent>
</local:CustomFixedDocument>
</Window.Resources>
<Grid>
<DocumentViewer x:Name="viewer" Document="{Binding Source={StaticResource Report}, Path=FixedDocument}"/>
</Grid>
现在这两个问题都得到了解决。有没有编译错误的实时设计时预览,并且可以打印输出。