代码说明:我要实现一个这样的功能 有三个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 Brush17 {18 get { return brush; }19 set20 {21 if (value != brush)22 {23 brush = value;24 NotifyPropertyChanged(() => 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 (Expression > 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 }
给Label标签绑定数据源 窗体初始化的时候绑定三遍就可以了 绑定完以后 直接设置就行了
Binding bind = new Binding(); bind.Source = ButtonBase.getButtonBase(); bind.Mode = BindingMode.TwoWay; bind.Path = new PropertyPath("Brush"); label1.SetBinding(Label.ForegroundProperty, bind);
先添加引用 C:\Program Files (x86)\Microsoft SDKs\Expression\Blend\.NETFramework\v4.5\Libraries\System.Windows.Interactivity.dll
using System.Windows.Interactivity;定义一个UIElement拖动的行为
1 public class DragInCanvasBehavior : Behavior2 { 3 private Canvas canvas; 4 private bool isDragOn = false; 5 private Point mouseOffset; 6 protected override void OnAttached() 7 { 8 base.OnAttached(); 9 this.AssociatedObject.MouseLeftButtonDown += AssociatedObject_MouseLeftButtonDown;10 this.AssociatedObject.MouseLeftButtonUp += AssociatedObject_MouseLeftButtonUp;11 this.AssociatedObject.MouseMove += AssociatedObject_MouseMove;12 }13 protected override void OnDetaching()14 {15 base.OnDetaching();16 this.AssociatedObject.MouseLeftButtonDown -= AssociatedObject_MouseLeftButtonDown;17 this.AssociatedObject.MouseLeftButtonUp -= AssociatedObject_MouseLeftButtonUp;18 this.AssociatedObject.MouseMove -= AssociatedObject_MouseMove;19 }20 void AssociatedObject_MouseMove(object sender, MouseEventArgs e)21 {22 if (isDragOn)23 {24 Point point = e.GetPosition(canvas);25 AssociatedObject.SetValue(Canvas.LeftProperty,point.X);26 AssociatedObject.SetValue(Canvas.TopProperty,point.Y);27 }28 }29 30 void AssociatedObject_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)31 {32 if (isDragOn)33 {34 AssociatedObject.ReleaseMouseCapture();35 isDragOn = false; 36 }37 }38 39 void AssociatedObject_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)40 {41 if (canvas == null)42 canvas = (Canvas)VisualTreeHelper.GetParent(this.AssociatedObject);43 isDragOn = true;44 mouseOffset = e.GetPosition(AssociatedObject);45 AssociatedObject.CaptureMouse();46 }47 48 }
前台使用该行为
1 xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 2 3
1
App.xmal.cs 后台代码
1 private void button_MouseEnter(object sender, MouseEventArgs e)2 {3 (sender as Button).Background = new SolidColorBrush(Colors.Brown);4 }5 private void button_MouseLeave(object sender, MouseEventArgs e)6 {7 (sender as Button).Background = null;8 }