66using System . Windows . Controls ;
77using System . Windows . Media . Animation ;
88using Automation ;
9+ using Catel . Logging ;
910using Services ;
1011
12+ [ StyleTypedProperty ( Property = nameof ( TextBlockStyle ) , StyleTargetType = typeof ( TextBlock ) ) ]
1113public class AnimatingTextBlock : UserControl , IStatusRepresenter
1214{
15+ private static readonly ILog Log = LogManager . GetCurrentClassLogger ( ) ;
16+
17+ private static readonly Storyboard DefaultShowStoryboard ;
18+ private static readonly Storyboard DefaultHideStoryboard ;
19+
1320 private Grid ? _contentGrid ;
1421 private int _currentIndex = 0 ;
1522
23+ static AnimatingTextBlock ( )
24+ {
25+ var showStoryboard = new Storyboard ( ) ;
26+
27+ var showAnimation = new DoubleAnimation
28+ {
29+ To = 1d ,
30+ Duration = TimeSpan . FromMilliseconds ( 500 ) ,
31+ EasingFunction = new CubicEase { EasingMode = EasingMode . EaseOut }
32+ } ;
33+
34+ Storyboard . SetTargetProperty ( showAnimation , new PropertyPath ( nameof ( TextBlock . Opacity ) ) ) ;
35+
36+ showStoryboard . Children . Add ( showAnimation ) ;
37+
38+ DefaultShowStoryboard = showStoryboard ;
39+
40+ var hideStoryboard = new Storyboard ( ) ;
41+
42+ var hideAnimation = new DoubleAnimation
43+ {
44+ To = 0d ,
45+ Duration = TimeSpan . FromMilliseconds ( 500 ) ,
46+ EasingFunction = new CubicEase { EasingMode = EasingMode . EaseOut }
47+ } ;
48+
49+ Storyboard . SetTargetProperty ( hideAnimation , new PropertyPath ( nameof ( TextBlock . Opacity ) ) ) ;
50+
51+ hideStoryboard . Children . Add ( hideAnimation ) ;
52+
53+ DefaultHideStoryboard = hideStoryboard ;
54+ }
55+
1656 public AnimatingTextBlock ( )
1757 {
58+ SetCurrentValue ( ShowStoryboardProperty , DefaultShowStoryboard ) ;
59+ SetCurrentValue ( HideStoryboardProperty , DefaultHideStoryboard ) ;
60+
1861 Loaded += OnLoaded ;
1962 }
2063
2164 #region Dependency properties
65+
66+
67+ public Style ? TextBlockStyle
68+ {
69+ get { return ( Style ? ) GetValue ( TextBlockStyleProperty ) ; }
70+ set { SetValue ( TextBlockStyleProperty , value ) ; }
71+ }
72+
73+ public static readonly DependencyProperty TextBlockStyleProperty = DependencyProperty . Register ( nameof ( TextBlockStyle ) ,
74+ typeof ( Style ) , typeof ( AnimatingTextBlock ) , new PropertyMetadata ( null ) ) ;
75+
76+
77+
2278 public string Text
2379 {
2480 get { return ( string ) GetValue ( TextProperty ) ; }
@@ -27,15 +83,27 @@ public string Text
2783
2884 public static readonly DependencyProperty TextProperty = DependencyProperty . Register ( nameof ( Text ) , typeof ( string ) , typeof ( AnimatingTextBlock ) ,
2985 new PropertyMetadata ( string . Empty , ( sender , _ ) => ( ( AnimatingTextBlock ) sender ) . OnTextChanged ( ) ) ) ;
30-
86+
87+
88+ public TextAlignment TextAlignment
89+ {
90+ get { return ( TextAlignment ) GetValue ( TextAlignmentProperty ) ; }
91+ set { SetValue ( TextAlignmentProperty , value ) ; }
92+ }
93+
94+ public static readonly DependencyProperty TextAlignmentProperty = DependencyProperty . Register ( nameof ( TextAlignment ) , typeof ( TextAlignment ) ,
95+ typeof ( AnimatingTextBlock ) , new PropertyMetadata ( TextAlignment . Left ) ) ;
96+
97+
3198 public Storyboard ? HideStoryboard
3299 {
33100 get { return ( Storyboard ? ) GetValue ( HideStoryboardProperty ) ; }
34101 set { SetValue ( HideStoryboardProperty , value ) ; }
35102 }
36103
37104 public static readonly DependencyProperty HideStoryboardProperty =
38- DependencyProperty . Register ( nameof ( HideStoryboard ) , typeof ( Storyboard ) , typeof ( AnimatingTextBlock ) , new PropertyMetadata ( null ) ) ;
105+ DependencyProperty . Register ( nameof ( HideStoryboard ) , typeof ( Storyboard ) , typeof ( AnimatingTextBlock ) , new PropertyMetadata ( ) ) ;
106+
39107
40108 public Storyboard ? ShowStoryboard
41109 {
@@ -44,7 +112,7 @@ public Storyboard? ShowStoryboard
44112 }
45113
46114 public static readonly DependencyProperty ShowStoryboardProperty =
47- DependencyProperty . Register ( nameof ( ShowStoryboard ) , typeof ( Storyboard ) , typeof ( AnimatingTextBlock ) , new PropertyMetadata ( null ) ) ;
115+ DependencyProperty . Register ( nameof ( ShowStoryboard ) , typeof ( Storyboard ) , typeof ( AnimatingTextBlock ) , new PropertyMetadata ( ) ) ;
48116
49117 #endregion
50118
@@ -97,7 +165,8 @@ private void OnHideStoryboardComplete(object? sender, EventArgs e)
97165
98166 private void OnHideComplete ( )
99167 {
100- if ( string . IsNullOrEmpty ( Text ) )
168+ var text = Text ;
169+ if ( string . IsNullOrEmpty ( text ) )
101170 {
102171 return ;
103172 }
@@ -114,7 +183,7 @@ private void OnHideComplete()
114183 }
115184
116185 var textBlockToShow = ( TextBlock ) _contentGrid . Children [ _currentIndex ] ;
117- textBlockToShow . SetCurrentValue ( TextBlock . TextProperty , Text ) ;
186+ textBlockToShow . SetCurrentValue ( TextBlock . TextProperty , text ) ;
118187
119188 var showStoryboard = ShowStoryboard ;
120189 if ( showStoryboard is not null )
@@ -162,16 +231,26 @@ private Grid CreateContent()
162231 var renderTransform = RenderTransform ;
163232 SetCurrentValue ( RenderTransformProperty , null ) ;
164233
234+ var textBlockStyle = TextBlockStyle ;
235+
165236 for ( var i = 0 ; i < 2 ; i ++ )
166237 {
167238 var textBlock = new TextBlock
168239 {
169- HorizontalAlignment = HorizontalAlignment . Left ,
170- VerticalAlignment = VerticalAlignment . Center ,
240+ HorizontalAlignment = HorizontalContentAlignment ,
241+ VerticalAlignment = VerticalContentAlignment ,
171242 Opacity = 0d ,
172- RenderTransform = renderTransform
243+ RenderTransform = renderTransform ,
244+ TextAlignment = TextAlignment ,
245+ TextWrapping = TextWrapping . NoWrap ,
246+ TextTrimming = TextTrimming . CharacterEllipsis ,
173247 } ;
174248
249+ if ( textBlockStyle is not null )
250+ {
251+ textBlock . Style = textBlockStyle ;
252+ }
253+
175254 grid . Children . Add ( textBlock ) ;
176255 }
177256
0 commit comments