11using System . Text . RegularExpressions ;
2+ using System . Windows ;
23using System . Windows . Controls ;
34using System . Windows . Documents ;
45using System . Xml ;
6+
57namespace VisualHFT . UserControls
68{
79 /// <summary>
@@ -12,59 +14,110 @@ public partial class MetricTile : UserControl
1214 public MetricTile ( )
1315 {
1416 InitializeComponent ( ) ;
17+
18+ // Subscribe to DataContext changes to update tooltip
19+ DataContextChanged += MetricTile_DataContextChanged ;
20+ }
21+
22+ private void MetricTile_DataContextChanged ( object sender , DependencyPropertyChangedEventArgs e )
23+ {
24+ if ( e . NewValue is VisualHFT . ViewModel . vmTile tile )
25+ {
26+ // Subscribe to property changes
27+ tile . PropertyChanged += Tile_PropertyChanged ;
28+
29+ // Update tooltip immediately with current value
30+ UpdateTooltip ( tile . Tooltip ) ;
31+ }
32+
33+ if ( e . OldValue is VisualHFT . ViewModel . vmTile oldTile )
34+ {
35+ // Unsubscribe from old tile
36+ oldTile . PropertyChanged -= Tile_PropertyChanged ;
37+ }
1538 }
1639
17- private void TextBox_TextChanged ( object sender , TextChangedEventArgs e )
40+ private void Tile_PropertyChanged ( object sender , System . ComponentModel . PropertyChangedEventArgs e )
1841 {
19- TextBox textBox = sender as TextBox ;
20- if ( textBox != null )
42+ if ( e . PropertyName == nameof ( VisualHFT . ViewModel . vmTile . Tooltip ) )
2143 {
22- ConvertHtmlToTextBlock ( textBox . Text , txtToolTip ) ;
44+ var tile = sender as VisualHFT . ViewModel . vmTile ;
45+ UpdateTooltip ( tile ? . Tooltip ) ;
2346 }
2447 }
48+
49+ private void UserControl_Loaded ( object sender , RoutedEventArgs e )
50+ {
51+ // Update tooltip when control loads
52+ if ( DataContext is VisualHFT . ViewModel . vmTile tile )
53+ {
54+ UpdateTooltip ( tile . Tooltip ) ;
55+ }
56+ }
57+
58+ private void UpdateTooltip ( string htmlText )
59+ {
60+ if ( string . IsNullOrEmpty ( htmlText ) )
61+ {
62+ txtToolTip . Inlines . Clear ( ) ;
63+ return ;
64+ }
65+
66+ ConvertHtmlToTextBlock ( htmlText , txtToolTip ) ;
67+ }
68+
2569 public TextBlock ConvertHtmlToTextBlock ( string htmlText , TextBlock textBlock )
2670 {
2771 htmlText = XmlEscapeAmpersands ( htmlText ) ;
2872
73+ textBlock . Inlines . Clear ( ) ; // Clear existing inlines
2974 textBlock . TextWrapping = System . Windows . TextWrapping . Wrap ;
3075 textBlock . Width = 500 ;
3176
32- XmlDocument doc = new XmlDocument ( ) ;
33- doc . LoadXml ( "<root>" + htmlText + "</root>" ) ;
34-
35- foreach ( XmlNode node in doc . DocumentElement . ChildNodes )
77+ try
3678 {
37- if ( node . NodeType == XmlNodeType . Text )
38- {
39- textBlock . Inlines . Add ( new Run ( node . Value ) ) ;
40- }
41- else if ( node . Name == "br" )
42- {
43- textBlock . Inlines . Add ( new LineBreak ( ) ) ;
44- }
45- else if ( node . Name == "b" )
46- {
47- Run run = new Run ( node . InnerText ) ;
48- run . FontWeight = System . Windows . FontWeights . Bold ;
49- textBlock . Inlines . Add ( run ) ;
50- }
51- else if ( node . Name == "i" )
79+ XmlDocument doc = new XmlDocument ( ) ;
80+ doc . LoadXml ( "<root>" + htmlText + "</root>" ) ;
81+
82+ foreach ( XmlNode node in doc . DocumentElement . ChildNodes )
5283 {
53- Run run = new Run ( node . InnerText ) ;
54- run . FontStyle = System . Windows . FontStyles . Italic ;
55- textBlock . Inlines . Add ( run ) ;
84+ if ( node . NodeType == XmlNodeType . Text )
85+ {
86+ textBlock . Inlines . Add ( new Run ( node . Value ) ) ;
87+ }
88+ else if ( node . Name == "br" )
89+ {
90+ textBlock . Inlines . Add ( new LineBreak ( ) ) ;
91+ }
92+ else if ( node . Name == "b" )
93+ {
94+ Run run = new Run ( node . InnerText ) ;
95+ run . FontWeight = System . Windows . FontWeights . Bold ;
96+ textBlock . Inlines . Add ( run ) ;
97+ }
98+ else if ( node . Name == "i" )
99+ {
100+ Run run = new Run ( node . InnerText ) ;
101+ run . FontStyle = System . Windows . FontStyles . Italic ;
102+ textBlock . Inlines . Add ( run ) ;
103+ }
104+ // Add more formatting cases as needed
56105 }
57- // Add more formatting cases as needed
106+ }
107+ catch ( XmlException )
108+ {
109+ // If HTML parsing fails, show raw text
110+ textBlock . Inlines . Add ( new Run ( htmlText ) ) ;
58111 }
59112
60113 return textBlock ;
61114 }
115+
62116 private static string XmlEscapeAmpersands ( string s )
63117 {
64118 if ( string . IsNullOrEmpty ( s ) ) return s ;
65119 // Replace & not starting a valid entity with &
66120 return Regex . Replace ( s , "&(?!(?:#\\ d+|#x[0-9A-Fa-f]+|[A-Za-z][A-Za-z0-9]*);)" , "&" ) ;
67121 }
68-
69122 }
70123}
0 commit comments