XAML FlowDocument with multiple Paragraph's from list of strings (Binding)

233 Views Asked by At

I need to display list of objects in FlowDocument. I don't want to add it programmatically, but want to pass this list of objects via binding. For example:

<RichTextBox>
    <FlowDocument>        
        <ItemsControl ItemsSource="{Binding Lines}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Paragraph>
                        <TextBox Text="{Binding Text}" />
                    </Paragraph>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>        
    </FlowDocument>
</RichTextBox>

There list of objects contains simple object like:

public class Line
{
   public string Text { get; set; }
}

Thank you for your time and help!

Best regards, Alex

1

There are 1 best solutions below

3
Umer Imtiaz On

You can use the code below:

<RichTextBox>
    <FlowDocument> 
<BlockUIContainer>
<ListView x:Name="myListView" ItemsSource="{Binding Lines}">
        <ListView.ItemTemplate>
            <DataTemplate>
                    <TextBlock Text="{Binding Text}" />
                   
            </DataTemplate>
        </ListView.ItemTemplate>
</ListView>
</BlockUIContainer>
</FlowDocument>  
</RichTextBox>

Lines should be of type IEnumerable/ObservableCollection.