简体   繁体   中英

Cannot Bind FlowDocument to RichTextBox in WPF

Here is my Xaml for RichTextBox

  <RichTextBox HorizontalAlignment="Left" Height="115" Margin="359,194,0,0" VerticalAlignment="Top"    Width="306" x:Name="missingVariablesRichTextBox" >
        <FlowDocument>
            <Paragraph>
               <ItemsControl  ItemsSource="{Binding}"/>
            </Paragraph>
        </FlowDocument>
    </RichTextBox>

Here is my FlowDocument from CodeBehind. Note message builder is a stringbuilder variable that has some string contents in it.

FlowDocument missingVariablesDoc = new FlowDocument();
missingVariablesDoc.Blocks.Add(new Paragraph(new Run(messageBuilder.ToString())));
missingVariablesRichTextBox.VerticalScrollBarVisibility=ScrollBarVisibility.Auto;
missingVariablesRichTextBox.DataContext=missingVariablesDoc;

When i run the app. I can't see anything in the richtextbox. What am i missing in the XAML databinding?

Any suggestions?

When you create a FlowDocument in code behind you certainly want to assign it to the Document property of the RichTextBox:

missingVariablesRichTextBox.Document = missingVariablesDoc;

Of course this replaces the FlowDocument assigned in XAML.

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