mirror of
https://github.com/ckaczor/Common.Wpf.git
synced 2026-01-14 09:35:38 -05:00
Initial commit
This commit is contained in:
57
HtmlTextBlock/HtmlTextBlock.cs
Normal file
57
HtmlTextBlock/HtmlTextBlock.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Documents;
|
||||
|
||||
namespace Common.Wpf.HtmlTextBlock
|
||||
{
|
||||
public class HtmlTextBlock : TextBlock
|
||||
{
|
||||
public static readonly DependencyProperty HtmlProperty = DependencyProperty.Register("Html", typeof(string), typeof(HtmlTextBlock), new UIPropertyMetadata("Html", OnHtmlChanged));
|
||||
|
||||
public static void OnHtmlChanged(DependencyObject s, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
HtmlTextBlock htmlTextBlock = (HtmlTextBlock) s;
|
||||
|
||||
Parse(htmlTextBlock, e.NewValue as string);
|
||||
}
|
||||
|
||||
private static void Parse(HtmlTextBlock control, string value)
|
||||
{
|
||||
try
|
||||
{
|
||||
control.Inlines.Clear();
|
||||
|
||||
TextParser parser = new TextParser();
|
||||
var lines = parser.Parse(control, value);
|
||||
|
||||
foreach (TextLine line in lines)
|
||||
{
|
||||
foreach (TextFragment fragment in line.FragmentList)
|
||||
{
|
||||
Run run = new Run(fragment.Text);
|
||||
|
||||
run.FontStyle = fragment.Style;
|
||||
run.FontWeight = fragment.Weight;
|
||||
run.FontSize = fragment.Size;
|
||||
run.Foreground = fragment.Color;
|
||||
|
||||
control.Inlines.Add(run);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
control.Inlines.Clear();
|
||||
|
||||
control.Text = value;
|
||||
}
|
||||
}
|
||||
|
||||
public string Html
|
||||
{
|
||||
get { return (string) GetValue(HtmlProperty); }
|
||||
set { SetValue(HtmlProperty, value); }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user