博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
wpf样式绑定 行为绑定 事件关联 路由事件实例
阅读量:5291 次
发布时间:2019-06-14

本文共 5389 字,大约阅读时间需要 17 分钟。

代码说明:我要实现一个这样的功能  有三个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 }
View Code

给Label标签绑定数据源  窗体初始化的时候绑定三遍就可以了  绑定完以后  直接设置就行了

Binding bind = new Binding();            bind.Source = ButtonBase.getButtonBase();            bind.Mode = BindingMode.TwoWay;            bind.Path = new PropertyPath("Brush");            label1.SetBinding(Label.ForegroundProperty, bind);
View Code

行为绑定

先添加引用 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
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 }
View Code

前台使用该行为

1  xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 2  3 
4
5
6
7
8
9
10
11
12
13
14
15
16
17
22
View Code

事件关联   当鼠标放到button上面的时候字体变大  button背景颜色变化

1 
View Code

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         }
View Code

 

转载于:https://www.cnblogs.com/zhan/p/3808539.html

你可能感兴趣的文章
【转】oracle case ,loop,while ,for简单实例
查看>>
Linux 操作系统启动流程
查看>>
将Express生成器下的pug修改为html
查看>>
[LeetCode]Spiral Matrix 54
查看>>
asp.net session mode 几种状态 (转)
查看>>
nodejs中异步
查看>>
疯狂使用 leancloud (投稿文章)
查看>>
SQL Server ON条件和WHERE条件
查看>>
[C5] Andrew Ng - Structuring Machine Learning Projects
查看>>
try-catch-finally中return的执行情况分析
查看>>
python 学习之路-day1
查看>>
【实例分解二】angularjs根据路由按需加载Controller
查看>>
python-函数-生成器-迭代器
查看>>
ajaxfileupload上传文件出现SyntaxError:unexpected token <错误
查看>>
day64 url用法以及django的路由系统
查看>>
圣杯布局跟双飞翼布局
查看>>
最简洁的IP判断正则表达式
查看>>
[三下五除二]在Eclipse上的JFinal_Demo
查看>>
神经网络计算异或
查看>>
Codeforces 785D Anton and School - 2 (范德蒙恒等式+ 乘法逆元)
查看>>