I will show you a sample of RichTextBox with MVVM. Here is my code for you:
XAMl code:
<Window.DataContext>
<local:ViewModel></local:ViewModel>
</Window.DataContext>
<StackPanel>
<TextBox Text="{Binding model.ID}" Width="200" Height="40"/>
<TextBox Text="{Binding model.Name}" Width="200" Height="40" Margin="0 20 0 20"/>
<RichTextBox Name="rtf" local:RichTextBoxHelper.DocumentXaml="{Binding model.Description}" Width="200" Height="40" />
<WrapPanel VerticalAlignment="Center" Width="100">
<ToggleButton x:Name="BoldButton" Command="EditingCommands.ToggleBold" CommandTarget="{Binding ElementName=rtf}" ToolTip="Bold" Width="20" Margin="0,1,0,1">
<Image Source="/fontbold.png" Stretch="None" SnapsToDevicePixels="True" />
</ToggleButton>
<ToggleButton x:Name="ItalicButton" Command="EditingCommands.ToggleItalic" CommandTarget="{Binding ElementName=rtf}" ToolTip="Bold" Width="20" Margin="0,1,0,1">
<Image Source="/fontitalic.png" Stretch="None" SnapsToDevicePixels="True" />
</ToggleButton>
</WrapPanel>
</StackPanel>
RichTextBoxHelper.cs code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Markup;
namespace RichTextBoxMVVM
{
public class RichTextBoxHelper : DependencyObject
{
private static HashSet<Thread> _recursionProtection = new HashSet<Thread>();
public static string GetDocumentXaml(DependencyObject obj)
{
return (string)obj.GetValue(DocumentXamlProperty);
}
public static void SetDocumentXaml(DependencyObject obj, string value)
{
_recursionProtection.Add(Thread.CurrentThread);
obj.SetValue(DocumentXamlProperty, value);
_recursionProtection.Remove(Thread.CurrentThread);
}
public static readonly DependencyProperty DocumentXamlProperty = DependencyProperty.RegisterAttached(
"DocumentXaml",
typeof(string),
typeof(RichTextBoxHelper),
new FrameworkPropertyMetadata(
"",
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
(obj, e) => {
if (_recursionProtection.Contains(Thread.CurrentThread))
return;
var richTextBox = (RichTextBox)obj;
try
{
var stream = new MemoryStream(Encoding.UTF8.GetBytes(GetDocumentXaml(richTextBox)));
var doc = (FlowDocument)XamlReader.Load(stream);
richTextBox.Document = doc;
}
catch (Exception)
{
richTextBox.Document = new FlowDocument();
}
richTextBox.TextChanged += (obj2, e2) =>
{
RichTextBox richTextBox2 = obj2 as RichTextBox;
if (richTextBox2 != null)
{
SetDocumentXaml(richTextBox, XamlWriter.Save(richTextBox2.Document));
}
};
}
)
);
}
}
ViewModel.cs and Model.cs code:
public class ViewModel
{
public Model model { get; set; }
public ViewModel()
{
model = new Model()
{
ID = "001",
Name = "Daisy",
Description = "<FlowDocument xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"><Paragraph Foreground=\"Red\"><Bold>Hello, this is RichTextBox</Bold></Paragraph></FlowDocument>"
};
}
}
public class Model
{
public string ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
The result picture is:
By the way, you may need to improve the way you ask questions. It's better for you to make your question clear without excess irrelevant code, and it not a good way to ask multiple functions to be implemented on the one question. You could create a new question to ask your Speech problem.
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.