代码说明:我要实现一个这样的功能 有三个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 }
给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 : Behavior<UIElement> 2 { 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 <Canvas HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="279" Margin="10,138,0,0" RenderTransformOrigin="0.5,0.5"> 4 <Canvas.RenderTransform> 5 <TransformGroup> 6 <ScaleTransform/> 7 <SkewTransform AngleY="0.583"/> 8 <RotateTransform/> 9 <TranslateTransform Y="0.565"/> 10 </TransformGroup> 11 </Canvas.RenderTransform> 12 <Rectangle x:Name="dragRec" Canvas.Left="10" Canvas.Right="10" Fill="Red" Width="40" Height="40"> 13 <i:Interaction.Behaviors> 14 <loc:DragInCanvasBehavior></loc:DragInCanvasBehavior> 15 </i:Interaction.Behaviors> 16 </Rectangle> 17 <Label Content="abcdefg"> 18 <i:Interaction.Behaviors> 19 <loc:DragInCanvasBehavior></loc:DragInCanvasBehavior> 20 </i:Interaction.Behaviors> 21 </Label> 22 </Canvas>
事件关联 当鼠标放到button上面的时候字体变大 button背景颜色变化
1 <Style x:Key="buttonStyle"> 2 <EventSetter Event="Button.MouseEnter" Handler="button_MouseEnter"></EventSetter> 3 <EventSetter Event="Button.MouseLeave" Handler="button_MouseLeave"></EventSetter> 4 <Style.Triggers> 5 <EventTrigger RoutedEvent="Mouse.MouseEnter"> 6 <EventTrigger.Actions> 7 <BeginStoryboard> 8 <Storyboard> 9 <DoubleAnimation Duration="0:0:0.2" Storyboard.TargetProperty="FontSize" To="22"> 10 </DoubleAnimation> 11 </Storyboard> 12 </BeginStoryboard> 13 </EventTrigger.Actions> 14 </EventTrigger> 15 <EventTrigger RoutedEvent="Mouse.MouseLeave"> 16 <EventTrigger.Actions> 17 <BeginStoryboard> 18 <Storyboard> 19 <DoubleAnimation Duration="0:0:0.1" Storyboard.TargetProperty="FontSize"> 20 </DoubleAnimation> 21 </Storyboard> 22 </BeginStoryboard> 23 </EventTrigger.Actions> 24 </EventTrigger> 25 </Style.Triggers> 26 </Style>
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 }