简体   繁体   中英

WPF/C# - Create FlowDocument Programmatically from XAML?

I'm kind of new to this FlowDocument thing, so I'm perfectly willing to accept that I'm doing something wrong. With that said, I have written a FlowDocument which exists within my project as a XAML file.

It's very simple so far as I have just started on it:

<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>

Now what I would like to do, via my code, is to get a reference to that document so I can set the binding to a model in order to set the image source. Can someone please point me in the direction of how to instanciate and load a FlowDocument in the code behind?

var flowDocument = 
    (FlowDocument)Application.LoadComponent(
        new Uri(@"SomeFlowDocument.xaml", UriKind.Relative));

flowDocument.DataContext = this;

Dispatcher.CurrentDispatcher.Invoke(
    DispatcherPriority.SystemIdle,
    new DispatcherOperationCallback(arg => null ), null);

See this answer for an explanation of that last bit.

It shows how to load the document from a resource and add content using bindings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM