开发者

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>
    开发者_Python百科            <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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜