c# - WPF/C# - 从 XAML 以编程方式创建 FlowDocument?

标签 c# wpf flowdocument

我对 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/

相关文章:

c# - string.GetHashCode 和 IEqualityComparer<string>.Default.GetHashCode 之间的区别

wpf - 当用户控件更改可见性时关注文本框

wpf - 在WPF中加载/保存RTF时保留字体大小

wpf - WPF MVVM-在TreeViewItem中触发事件的最正确方法是什么

c# - 使用 WPF 显示流式富文本

c# - 使用 XamlReader 和 XamlWriter 时将一个 FlowDocument 的内容插入另一个

C# NPOI 库和无效的 zip 压缩

c# - 如何在 Windows 商店应用程序中实现登录注销功能?

c# - Moles 以代码 -1002 退出,我怎么知道它发生了什么?

c# - 多重绑定(bind)作为 XAML 中的资源