我对 FlowDocument 有点陌生,所以我完全愿意接受我做错了什么。话虽如此,我已经编写了一个 FlowDocument,它作为 XAML 文件存在于我的项目中。
就我刚刚开始而言,这非常简单:
<FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
ColumnWidth="400" FontSize="14" FontFamily="Georgia">
<Table>
<Table.Columns>
<TableColumn Width="*" />
<TableColumn Width="*" />
<TableColumn Width="*" />
</Table.Columns>
<TableRowGroup>
<TableRow>
<TableCell>
<BlockUIContainer>
<Image Source="{Binding Logo}" />
</BlockUIContainer>
</TableCell>
</TableRow>
</TableRowGroup>
</Table>
</FlowDocument>
现在我想做的是,通过我的代码获取对该文档的引用,以便我可以将绑定(bind)设置到模型以设置图像源。有人可以指出如何在后面的代码中实例化和加载 FlowDocument 的方向吗?
最佳答案
var flowDocument =
(FlowDocument)Application.LoadComponent(
new Uri(@"SomeFlowDocument.xaml", UriKind.Relative));
flowDocument.DataContext = this;
Dispatcher.CurrentDispatcher.Invoke(
DispatcherPriority.SystemIdle,
new DispatcherOperationCallback(arg => null ), null);
参见this answer了解最后一点的解释。
它展示了如何从资源加载文档并使用绑定(bind)添加内容。
关于c# - WPF/C# - 从 XAML 以编程方式创建 FlowDocument?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6034892/