截 图
Code:
View Code
15 6 247 19 20 2321 22
View Code
1 ///2 /// 画布距离类 3 /// 实现了INotifyPropertyChanged接口 4 /// 5 public class CanvasLength : INotifyPropertyChanged 6 { 7 8 #region Property 9 10 //左边距 11 private int canvasLeft; 12 ///13 /// 左边距 14 /// 15 public int CanvasLeft 16 { 17 get { return canvasLeft; } 18 set 19 { 20 canvasLeft = value; 21 NotifyPropertyChanged("CanvasLeft"); 22 } 23 } 24 #endregion 25 26 #region Method 27 28 public event PropertyChangedEventHandler PropertyChanged; 29 void NotifyPropertyChanged(string str) 30 { 31 if (PropertyChanged != null) 32 { 33 PropertyChanged(this, new PropertyChangedEventArgs(str)); 34 } 35 } 36 37 #endregion 38 }
View Code
1 ///2 /// Bindingwithxaml.xaml 的交互逻辑 3 /// 4 public partial class Bindingwithxaml : Window 5 { 6 #region Field 7 //计时器 8 DispatcherTimer myDispatcherTimer; 9 //CanvasLength类 10 CanvasLength CLength; 11 12 #endregion 13 14 #region Property 15 16 private int canvasleft; 17 18 public int Canvasleft 19 { 20 get { return canvasleft; } 21 set 22 { 23 canvasleft = value; 24 } 25 } 26 #endregion 27 #region Method 28 29 public Bindingwithxaml() 30 { 31 InitializeComponent(); 32 myDispatcherTimer = new DispatcherTimer(); 33 CLength = new CanvasLength(); 34 //为PropertyChanged事件绑定方法 35 CLength.PropertyChanged += new PropertyChangedEventHandler(CLength_PropertyChanged); 36 //为Tick事件绑定方法 37 myDispatcherTimer.Tick += new EventHandler(myDispatcherTimer_Tick); 38 //设置myDispatcherTimer的时间段为200毫秒 39 myDispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 200); 40 //启动myDispatcherTimer 41 myDispatcherTimer.Start(); 42 43 } 44 45 ///46 /// CLength.PropertyChanged事件绑定方法 47 /// 48 void CLength_PropertyChanged(object sender, PropertyChangedEventArgs e) 49 { 50 //设置slider的Value值 51 //this.slider.Value = CLength.CanvasLeft; 52 Canvasleft = CLength.CanvasLeft; 53 } 54 55 ///56 /// myDispatcherTimer.Tick事件绑定方法 57 /// 58 void myDispatcherTimer_Tick(object sender, EventArgs e) 59 { 60 //设置CLength的CanvasLeft值 61 CLength.CanvasLeft += 10; 62 } 63 64 #endregion 65 }