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!
wpf binding richtextbox
add a comment |
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!
wpf binding richtextbox
add a comment |
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!
wpf binding richtextbox
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
wpf binding richtextbox
asked Nov 5 at 18:00
DR_Bart
63
63
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 6 at 10:00
answered Nov 5 at 23:41
Congenital Optimist
8518
8518
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password