How about setting the Foreground
and FontSize
for the FlowDocument
in the converter? I make a demo for it.
<Window.Resources>
<local:FlowDocumentToXamlConverter x:Key="flowDocumentConverter"/>
</Window.Resources>
<Window.DataContext>
<local:MainWindowViewModel></local:MainWindowViewModel>
</Window.DataContext>
<local:MyRichTextBox Grid.Row="0" x:Name="EditBox" Document="{Binding Path=DocumentXaml, Converter={StaticResource flowDocumentConverter}, Mode=TwoWay}"/>
The code for converter is:
[ValueConversion(typeof(string), typeof(FlowDocument))]
public class FlowDocumentToXamlConverter : IValueConverter
{
public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var flowDocument = new FlowDocument();
if (value != null)
{
var xamlText = (string)value;
flowDocument = (FlowDocument)XamlReader.Parse(xamlText);
}
flowDocument.FontSize = 30;
flowDocument.FontStyle = FontStyles.Italic;
flowDocument.Foreground = Brushes.Red;
flowDocument.TextAlignment = TextAlignment.Center;
return flowDocument;
}
public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null) return string.Empty;
var flowDocument = (FlowDocument)value;
flowDocument.FontSize = 30;
flowDocument.FontStyle = FontStyles.Italic;
flowDocument.Foreground = Brushes.Red;
flowDocument.TextAlignment = TextAlignment.Center;
return XamlWriter.Save(flowDocument);
}
}
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.