博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
绑定元素属性改变不通知界面
阅读量:5965 次
发布时间:2019-06-19

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

原文:

情景假设:绑定的是一个Point,当Point的X或者Y属性发生改变时,绑定的点也随界面改变

此时界面不会发生改变

原因:当X或者Y属性发生改变时并没有触发Point的Set方法

 

1   
2
3
4
5 6
12
13 14 15
16
17
18
19 20 21
22
23 24
25
26
27
28
29
32
33
34
35
36
37
38 39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 69
71
72
1 private ObservableCollection
points = new ObservableCollection
(); 2 3 public ObservableCollection
Points 4 { 5 get { return points; } 6 set{points = value;} 7 } 8 9 10 11 12 13 14 public Point StartPoint 15 { 16 get { return (Point)GetValue(StartPointProperty); } 17 set { SetValue(StartPointProperty, value); } 18 } 19 20 // Using a DependencyProperty as the backing store for StartPoint. This enables animation, styling, binding, etc... 21 public static readonly DependencyProperty StartPointProperty = 22 DependencyProperty.Register("StartPoint", typeof(Point), typeof(PathAnimationDemo), new PropertyMetadata(new Point(0, 0))); 23 24 25 26 public Point EndPoint 27 { 28 get { return (Point)GetValue(EndPointProperty); } 29 set 30 { 31 SetValue(EndPointProperty, value); 32 this.OnPropertyChanged("EndPoint"); 33 } 34 } 35 36 // Using a DependencyProperty as the backing store for EndPoint. This enables animation, styling, binding, etc... 37 public static readonly DependencyProperty EndPointProperty = 38 DependencyProperty.Register("EndPoint", typeof(Point), typeof(PathAnimationDemo), new PropertyMetadata(new Point(100, 100))); 39 40 private double xStartPoint; 41 42 public double XStartPoint 43 { 44 get { return this.StartPoint.X; } 45 set 46 { 47 xStartPoint = value; 48 this.StartPoint = new Point(xStartPoint, this.StartPoint.X); 49 } 50 } 51 52 53 private double yStartPoint; 54 55 public double YStartPoint 56 { 57 get { return this.StartPoint.Y; } 58 set 59 { 60 yStartPoint = value; 61 this.StartPoint = new Point(this.StartPoint.X, yStartPoint); 62 } 63 } 64 65 private double xEndPoint; 66 67 public double XEndPoint 68 { 69 get { return this.EndPoint.X; } 70 set 71 { 72 xEndPoint = value; 73 this.EndPoint = new Point(xEndPoint, this.EndPoint.Y); 74 } 75 } 76 77 private double yEndPoint; 78 79 public double YEndPoint 80 { 81 get { return this.EndPoint.Y; } 82 set 83 { 84 yEndPoint = value; 85 this.EndPoint = new Point(this.EndPoint.X, yEndPoint); 86 } 87 } 88 89 90 private double xPoint; 91 92 public double XPoint 93 { 94 get { return xPoint; } 95 set { xPoint = value; } 96 } 97 98 private double yPoint; 99 100 public double YPoint101 {102 get { return yPoint; }103 set { yPoint = value; }104 }105 106 107 108 109 public PathAnimationDemo()110 {111 this.SetPoints(this.points);112 InitializeComponent();113 114 }115 116 private void SetPoints(ObservableCollection
myPointCollection)117 {118 points.Add(new Point(50, 100));119 myPointCollection.Add(new Point(100, 50));120 myPointCollection.Add(new Point(200, 100));121 myPointCollection.Add(new Point(100, 200));122 myPointCollection.Add(new Point(400, 400));123 myPointCollection.Add(new Point(600, 600));124 }125 126 public event PropertyChangedEventHandler PropertyChanged;127 128 private void OnPropertyChanged(string propertyName)129 {130 if (PropertyChanged != null)131 {132 PropertyChanged(this, new PropertyChangedEventArgs(propertyName));133 }134 }135 136 private void Button_Click(object sender, RoutedEventArgs e)137 {138 this.points.Add(new Point(this.xPoint, this.YPoint));139 this.Points = this.points;140 }141 142 }

 

 

转载地址:http://iztax.baihongyu.com/

你可能感兴趣的文章
知道双字节码, 如何获取汉字 - 回复 "pinezhou" 的问题
查看>>
Python高效编程技巧
查看>>
js中var self=this的解释
查看>>
Facebook 接入之获取各个配置参数
查看>>
linux的日志服务器关于屏蔽一些关键字的方法
查看>>
事情的两面性
查看>>
只要会营销,shi都能卖出去?
查看>>
sed单行处理命令奇偶行输出
查看>>
VC++深入详解学习笔记1
查看>>
安装配置discuz
查看>>
线程互互斥锁
查看>>
KVM虚拟机&openVSwitch杂记(1)
查看>>
win7下ActiveX注册错误0x80040200解决参考
查看>>
《.NET应用架构设计:原则、模式与实践》新书博客--试读-1.1-正确认识软件架构...
查看>>
2013 Linux领域年终盘点
查看>>
mysql多实例实例化数据库
查看>>
javascript 操作DOM元素样式
查看>>
Android 内存管理 &Memory Leak & OOM 分析
查看>>
HBase 笔记3
查看>>
【Linux】Linux 在线安装yum
查看>>