WPF RichTextBox Content Binding











up vote
0
down vote

favorite












I want to bind to a RichTextBox, where I can add Runs of text with formatting at run-time. I am using an MVVM pattern, so ideally this can be done from the ViewModel and not the code-behind.



Here is what I have so far:



<RichTextBox HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Grid.Row="0"
Grid.Column="0"
BorderThickness="4"

FontFamily="Consolas"
FontSize="{StaticResource TextBoxFontSize}"
FontWeight="Bold"
Background="Black"
>

<FlowDocument>
<Paragraph>
<Run Foreground="{Binding TextColour}"
Text="{Binding CmdText}" />

<!-- I want to add more text runs here! -->

</Paragraph>
</FlowDocument>

</RichTextBox>


The problem is that I don't know what method in the RTB I should bind to in order to add runs of text/paragraphs to the RTB at runtime. If I know I should be able to create the method to do this in the view model easily enough.



Any help is appreciated!



Thanks!










share|improve this question


























    up vote
    0
    down vote

    favorite












    I want to bind to a RichTextBox, where I can add Runs of text with formatting at run-time. I am using an MVVM pattern, so ideally this can be done from the ViewModel and not the code-behind.



    Here is what I have so far:



    <RichTextBox HorizontalAlignment="Stretch"
    VerticalAlignment="Stretch"
    Grid.Row="0"
    Grid.Column="0"
    BorderThickness="4"

    FontFamily="Consolas"
    FontSize="{StaticResource TextBoxFontSize}"
    FontWeight="Bold"
    Background="Black"
    >

    <FlowDocument>
    <Paragraph>
    <Run Foreground="{Binding TextColour}"
    Text="{Binding CmdText}" />

    <!-- I want to add more text runs here! -->

    </Paragraph>
    </FlowDocument>

    </RichTextBox>


    The problem is that I don't know what method in the RTB I should bind to in order to add runs of text/paragraphs to the RTB at runtime. If I know I should be able to create the method to do this in the view model easily enough.



    Any help is appreciated!



    Thanks!










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I want to bind to a RichTextBox, where I can add Runs of text with formatting at run-time. I am using an MVVM pattern, so ideally this can be done from the ViewModel and not the code-behind.



      Here is what I have so far:



      <RichTextBox HorizontalAlignment="Stretch"
      VerticalAlignment="Stretch"
      Grid.Row="0"
      Grid.Column="0"
      BorderThickness="4"

      FontFamily="Consolas"
      FontSize="{StaticResource TextBoxFontSize}"
      FontWeight="Bold"
      Background="Black"
      >

      <FlowDocument>
      <Paragraph>
      <Run Foreground="{Binding TextColour}"
      Text="{Binding CmdText}" />

      <!-- I want to add more text runs here! -->

      </Paragraph>
      </FlowDocument>

      </RichTextBox>


      The problem is that I don't know what method in the RTB I should bind to in order to add runs of text/paragraphs to the RTB at runtime. If I know I should be able to create the method to do this in the view model easily enough.



      Any help is appreciated!



      Thanks!










      share|improve this question













      I want to bind to a RichTextBox, where I can add Runs of text with formatting at run-time. I am using an MVVM pattern, so ideally this can be done from the ViewModel and not the code-behind.



      Here is what I have so far:



      <RichTextBox HorizontalAlignment="Stretch"
      VerticalAlignment="Stretch"
      Grid.Row="0"
      Grid.Column="0"
      BorderThickness="4"

      FontFamily="Consolas"
      FontSize="{StaticResource TextBoxFontSize}"
      FontWeight="Bold"
      Background="Black"
      >

      <FlowDocument>
      <Paragraph>
      <Run Foreground="{Binding TextColour}"
      Text="{Binding CmdText}" />

      <!-- I want to add more text runs here! -->

      </Paragraph>
      </FlowDocument>

      </RichTextBox>


      The problem is that I don't know what method in the RTB I should bind to in order to add runs of text/paragraphs to the RTB at runtime. If I know I should be able to create the method to do this in the view model easily enough.



      Any help is appreciated!



      Thanks!







      wpf binding richtextbox






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 5 at 18:00









      DR_Bart

      63




      63
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          If you look into the rich text box then it contains FlowDocument which again contains Blocks.



              <RichTextBox>
          <FlowDocument>
          <Paragraph>
          <Run>Text here!</Run>
          </Paragraph>
          </FlowDocument>
          </RichTextBox>


          To bind the RichTextBox to content one can create (derive) a control that exposes FlowDocument from RichTextBox as dependency property. In such case the Viemodel maintains the FlowDocument and it gets updated into the view - you bind it to the dpendency prop on your derived control.



          Now, text input will be a problem because changing content in the FlowDocument does not notify the viewmodel about changes. The Blocks in the FlowDocument should reside in the ObservableCollection in order, well, to be observable.



          In short, the RichTextBox is not very good at MVVM. If the editability is not needed, you could rather present your data from viewmodel as ObservableCollection containing INotifyPropertyChanged objects and present the structure using ItemsControl and DataTemplates.



          I'd like to add that doing something in code-behind is not against MVVM, as long as you maintain separation of model, view and viewmodel.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53159786%2fwpf-richtextbox-content-binding%23new-answer', 'question_page');
            }
            );

            Post as a guest
































            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            If you look into the rich text box then it contains FlowDocument which again contains Blocks.



                <RichTextBox>
            <FlowDocument>
            <Paragraph>
            <Run>Text here!</Run>
            </Paragraph>
            </FlowDocument>
            </RichTextBox>


            To bind the RichTextBox to content one can create (derive) a control that exposes FlowDocument from RichTextBox as dependency property. In such case the Viemodel maintains the FlowDocument and it gets updated into the view - you bind it to the dpendency prop on your derived control.



            Now, text input will be a problem because changing content in the FlowDocument does not notify the viewmodel about changes. The Blocks in the FlowDocument should reside in the ObservableCollection in order, well, to be observable.



            In short, the RichTextBox is not very good at MVVM. If the editability is not needed, you could rather present your data from viewmodel as ObservableCollection containing INotifyPropertyChanged objects and present the structure using ItemsControl and DataTemplates.



            I'd like to add that doing something in code-behind is not against MVVM, as long as you maintain separation of model, view and viewmodel.






            share|improve this answer



























              up vote
              0
              down vote













              If you look into the rich text box then it contains FlowDocument which again contains Blocks.



                  <RichTextBox>
              <FlowDocument>
              <Paragraph>
              <Run>Text here!</Run>
              </Paragraph>
              </FlowDocument>
              </RichTextBox>


              To bind the RichTextBox to content one can create (derive) a control that exposes FlowDocument from RichTextBox as dependency property. In such case the Viemodel maintains the FlowDocument and it gets updated into the view - you bind it to the dpendency prop on your derived control.



              Now, text input will be a problem because changing content in the FlowDocument does not notify the viewmodel about changes. The Blocks in the FlowDocument should reside in the ObservableCollection in order, well, to be observable.



              In short, the RichTextBox is not very good at MVVM. If the editability is not needed, you could rather present your data from viewmodel as ObservableCollection containing INotifyPropertyChanged objects and present the structure using ItemsControl and DataTemplates.



              I'd like to add that doing something in code-behind is not against MVVM, as long as you maintain separation of model, view and viewmodel.






              share|improve this answer

























                up vote
                0
                down vote










                up vote
                0
                down vote









                If you look into the rich text box then it contains FlowDocument which again contains Blocks.



                    <RichTextBox>
                <FlowDocument>
                <Paragraph>
                <Run>Text here!</Run>
                </Paragraph>
                </FlowDocument>
                </RichTextBox>


                To bind the RichTextBox to content one can create (derive) a control that exposes FlowDocument from RichTextBox as dependency property. In such case the Viemodel maintains the FlowDocument and it gets updated into the view - you bind it to the dpendency prop on your derived control.



                Now, text input will be a problem because changing content in the FlowDocument does not notify the viewmodel about changes. The Blocks in the FlowDocument should reside in the ObservableCollection in order, well, to be observable.



                In short, the RichTextBox is not very good at MVVM. If the editability is not needed, you could rather present your data from viewmodel as ObservableCollection containing INotifyPropertyChanged objects and present the structure using ItemsControl and DataTemplates.



                I'd like to add that doing something in code-behind is not against MVVM, as long as you maintain separation of model, view and viewmodel.






                share|improve this answer














                If you look into the rich text box then it contains FlowDocument which again contains Blocks.



                    <RichTextBox>
                <FlowDocument>
                <Paragraph>
                <Run>Text here!</Run>
                </Paragraph>
                </FlowDocument>
                </RichTextBox>


                To bind the RichTextBox to content one can create (derive) a control that exposes FlowDocument from RichTextBox as dependency property. In such case the Viemodel maintains the FlowDocument and it gets updated into the view - you bind it to the dpendency prop on your derived control.



                Now, text input will be a problem because changing content in the FlowDocument does not notify the viewmodel about changes. The Blocks in the FlowDocument should reside in the ObservableCollection in order, well, to be observable.



                In short, the RichTextBox is not very good at MVVM. If the editability is not needed, you could rather present your data from viewmodel as ObservableCollection containing INotifyPropertyChanged objects and present the structure using ItemsControl and DataTemplates.



                I'd like to add that doing something in code-behind is not against MVVM, as long as you maintain separation of model, view and viewmodel.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 6 at 10:00

























                answered Nov 5 at 23:41









                Congenital Optimist

                8518




                8518






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53159786%2fwpf-richtextbox-content-binding%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest




















































































                    這個網誌中的熱門文章

                    Xamarin.form Move up view when keyboard appear

                    Post-Redirect-Get with Spring WebFlux and Thymeleaf

                    Anylogic : not able to use stopDelay()