1
1
using System ;
2
+ using System . ComponentModel ;
3
+ using System . Reflection ;
4
+ using System . Runtime . InteropServices ;
2
5
using System . Windows ;
3
6
using System . Windows . Documents ;
4
7
using System . Windows . Media ;
8
+ using System . Xml . Linq ;
5
9
using WPFDevelopers . Helpers ;
6
10
7
11
namespace WPFDevelopers . Controls
8
12
{
9
13
public class Badge : Adorner
10
14
{
11
15
public static readonly DependencyProperty TextProperty =
12
- DependencyProperty . Register ( "Text" , typeof ( string ) , typeof ( Badge ) , new PropertyMetadata ( string . Empty ) ) ;
16
+ DependencyProperty . RegisterAttached ( "Text" , typeof ( string ) , typeof ( Badge ) , new PropertyMetadata ( string . Empty , OnTextChanged ) ) ;
13
17
14
18
public static readonly DependencyProperty FontSizeProperty =
15
19
DependencyProperty . Register ( "FontSize" , typeof ( double ) , typeof ( Badge ) , new PropertyMetadata ( 10.0d ) ) ;
16
20
17
-
18
21
public static readonly DependencyProperty IsShowProperty =
19
22
DependencyProperty . RegisterAttached ( "IsShow" , typeof ( bool ) , typeof ( Badge ) ,
20
23
new PropertyMetadata ( false , OnIsBadgeChanged ) ) ;
@@ -118,6 +121,21 @@ public static void SetHorizontalOffset(UIElement element, string horizontalOffse
118
121
element . SetValue ( HorizontalOffsetProperty , horizontalOffset ) ;
119
122
}
120
123
124
+ private static void OnTextChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
125
+ {
126
+ if ( d is FrameworkElement parent )
127
+ {
128
+ var isShow = GetIsShow ( parent ) ;
129
+ if ( ! isShow ) return ;
130
+ var newEventArgs = new DependencyPropertyChangedEventArgs (
131
+ e . Property ,
132
+ false ,
133
+ isShow ) ;
134
+ OnIsBadgeChanged ( d , newEventArgs ) ;
135
+ }
136
+
137
+ }
138
+
121
139
private static void OnIsBadgeChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
122
140
{
123
141
if ( e . NewValue is bool isShow && d is FrameworkElement parent )
@@ -140,7 +158,6 @@ private static void OnIsBadgeChanged(DependencyObject d, DependencyPropertyChang
140
158
}
141
159
private static void Parent_IsVisibleChanged ( object sender , DependencyPropertyChangedEventArgs e )
142
160
{
143
-
144
161
if ( e . NewValue is bool isVisible && sender is FrameworkElement parent )
145
162
{
146
163
var isShow = GetIsShow ( parent ) ;
0 commit comments