question

StoutGamer avatar image
0 Votes"
StoutGamer asked StoutGamer answered

How can I bind text and have it format itself on the UI?

Hi. How can I bind text and have it format itself on the UI?

For example, suppose the string in the ViewModel is this:

 myDependencyProperty = @"<b>Title: </b>My title\n<b>Description: </b>This is my description.\n";


and in the View layer I have this:

 <TextBlock Text="{Binding myDependencyProperty}" />

The problem is that it gets displayed as is. But I want it to be formatted accordingly, as follows:

Title: My Window

Description: This is my description.

How can I get that done?

Thank you.







windows-wpfdotnet-wpf-xaml
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

PeterFleischer-3316 avatar image
1 Vote"
PeterFleischer-3316 answered PeterFleischer-3316 edited

Hi,
you can use WebBrowser to display your string and replace NewLine (\n) in string. Try following demo:

XAML

 <Window x:Class="WpfApp1.Window023"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:local="clr-namespace:WpfApp1"
         mc:Ignorable="d"
         Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
   <Grid>
     <WebBrowser x:Name="wb"/>
   </Grid>
 </Window>

And CodeBehind:

 using System.Windows;
    
 namespace WpfApp1
 {
   public partial class Window023 : Window
   {
     public Window023()
     {
       InitializeComponent();
     }
    
     private void Window_Loaded(object sender, RoutedEventArgs e)
     {
       string str = @"<b>Title: </b>My title\n<b>Description: </b>This is my description.\n";
       str = str.Replace("\\n", "<br />");
       wb.NavigateToString(str);
     }
   }
 }

Result:

49468-x.png





x.png (7.4 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
1 Vote"
Viorel-1 answered

To show formatted text in WPF, you can use a RichTextBox, for example:

 <RichTextBox IsReadOnly="True" IsReadOnlyCaretVisible="True">
     <FlowDocument x:Name="doc1"/>
 </RichTextBox>
    
 // code:
 string t = "<Span xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xml:space='preserve'><Bold>Title: </Bold>My title<LineBreak/><Bold>Description: </Bold>This is my description.</Span>";
 var tr = new TextRange( doc1.ContentStart, doc1.ContentEnd );
 using( var sr = new MemoryStream( Encoding.UTF8.GetBytes( t ) ) )
 {
     tr.Load( sr, DataFormats.Xaml );
 }

However, this example does not use binding.

See also: DocumentViewer, FlowDocumentReader, FlowDocumentScrollViewer.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

StoutGamer avatar image
0 Votes"
StoutGamer answered

Hi. Thank you both; those answers and samples are extremely helpful. Works perfectly.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.