WPF 监听对象的某个属性

DependencyPropertyDescriptor descriptor = DependencyPropertyDescriptor.FromProperty(Canvas.LeftProperty, typeof(Image));
                        descriptor.AddValueChanged(img, (o, _e) =>
                        {
                            try
                            {
                                double left = Canvas.GetLeft(img);
                                //如果超出画布,则重新设定位置
                                if (left <= 0)
                                {
                                    Canvas.SetLeft(img, 0);
                                }
                                if (left + img.Width >= this.canvasEditor.Width)
                                {
                                    Canvas.SetLeft(img, this.canvasEditor.Width - img.Width);
                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message);
                            }
 
                        });

其中img为Image类型的变量

posted @ 2020-12-16 09:34  划破黑夜  阅读(516)  评论(0编辑  收藏  举报