I have a FlowDocument containing stuff bound to my ViewModel like this:
<FlowDocumentReader>
<FlowDocument>
<Paragraph>
<Run Text="{Binding MyTextProperty}"/>
</Paragraph>
</FlowDocument>
</FlowDocumentReader>
Now I want to display a List of class using some kind of DataTemplate, but got no idea how to start. Say I got a class like:
public MyClass
{
String Title {get;set;}
String FlowText {get;set;}
}
public List<MyClass> MyList {get;set;}
And I'd like to bind this to the FlowDocument List, like this:
<FlowDocumentReader>
<FlowDocument>
<List Items="{Binding MyList}">
<Bold><Run Text="{Binding Title}"/></Bold>
<LineBreak/>
<Run Text="{Binding FlowText}"/>
</Paragraph>
</FlowDocument>
</FlowDocumentReader>
Of course this does not work - but I can't find any explanation how to bind Lists in a FlowDocument using Templates - is this possible?
See this question.
I think you have two options
Update
A more Dynamic solution using two attached properties. A Resource with the Template is added to the Paragraph (which must have the
x:Shared="False"
attribute set, otherwise we'll just keep adding the same elements over and over). Then the Source List and the name of the template resource is set as attached properties.In the PropertyChanged callback, a check is made that both properties are set and then a
Span
element is created for each item in the List. The span elementsDataContext
is set to the current item to make the Bindings workParagraphInlineBehavior