代码说明:我要实现一个这样的功能  有三个window窗口  每个窗体有一个label标签  当我修改三个label标签中任意一个字体颜色的时候  其他的label标签字体颜色也变化

首先三个窗体不用贴代码了  直接添加三个就行了

样式绑定:

先添加数据源  代码如下: (注:为了防止propertyName硬编码写死   可以使用CallerMemberName附加属性来获取默认的属性名称 或者使用表达式目录树Expression<Func<T>>的方式来获取)

 1 public class ButtonBase : ContentControl, INotifyPropertyChanged
 2     {
 3         public static readonly RoutedEvent ClickEvent;
 4         private SolidColorBrush brush = new SolidColorBrush(Colors.Red);
 5 
 6         public event PropertyChangedEventHandler PropertyChanged;
 7 
 8         private static ButtonBase btnBase;
 9         
10         public static ButtonBase getButtonBase()
11         {
12             if (btnBase == null)
13                 btnBase = new ButtonBase() { Foreground = new SolidColorBrush(Colors.Red) };
14             return btnBase;
15         }
16         public SolidColorBrush Brush
17         {
18             get { return brush; }
19             set
20             {
21                 if (value != brush)
22                 {
23                     brush = value;
24                     NotifyPropertyChanged<SolidColorBrush>(() => this.Brush);//NotifyPropertyChanged();
25                 }
26             }
27         }
28         private void NotifyPropertyChanged([CallerMemberName] String PropertyName = "")
29         {
30             if (PropertyChanged != null)
31                 PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
32         }
33         private void NotifyPropertyChanged<T>(Expression<Func<T>> PropertyName)
34         {
35             if (PropertyChanged != null)
36             {
37                 var expressions = PropertyName.Body as MemberExpression;
38 
39                 PropertyChanged(this, new PropertyChangedEventArgs(expressions.Member.Name));
40             }
41         }
View Code

相关文章: