Binding to other control:Funny Face

截 图

Code:

View Code
 1 <Window x:Class="WpfTestApplication.Bindingwithxaml"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 Title="FunnyFace" Height="300" Width="300">
5 <Grid>
6 <StackPanel>
7 <Canvas Height="210" Width="280">
8 <Canvas Canvas.Top="0" Canvas.Right="{Binding Path=Value, ElementName=slider}" Name="FunnyFace" Height="210" Width="230">
9 <Ellipse Canvas.Left="20" Canvas.Top="50" Width="100" Height="100" Stroke="Blue" StrokeThickness="4" Fill="Yellow"/>
10 <Ellipse Canvas.Left="40" Canvas.Top="65" Width="25" Height="25" Stroke="Blue" StrokeThickness="3" Fill="White"/>
11 <Ellipse Canvas.Left="50" Canvas.Top="75" Width="5" Height="5" Fill="Black"/>
12 <Path Name="mouth" Stroke="Blue" StrokeThickness="4" Data="M 32,125 Q 65,122 72,108"/>
13 <Line X1="94" X2="102" Y1="144" Y2="166" Stroke="Blue" StrokeThickness="4"/>
14 <Line X1="84" X2="103" Y1="169" Y2="166" Stroke="Blue" StrokeThickness="4"/>
15 <Line X1="62" X2="52" Y1="164" Y2="168" Stroke="Blue" StrokeThickness="4"/>
16 <Line X1="38" X2="53" Y1="160" Y2="168" Stroke="Blue" StrokeThickness="4"/>
17 </Canvas>
18 </Canvas>
19 <!--<Slider Name="slider" Orientation="Horizontal" Value="10" Maximum="200"/>-->
20 <Slider Name="slider" Orientation="Horizontal" Value="{Binding Canvasleft}" Maximum="200"/>
21 <TextBlock Text="{Binding Canvasleft}"></TextBlock>
22 </StackPanel>
23 </Grid>
24 </Window>
View Code
 1 ///<summary>
2 /// 画布距离类
3 /// 实现了INotifyPropertyChanged接口
4 ///</summary>
5 public class CanvasLength : INotifyPropertyChanged
6 {
7
8 #region Property
9
10 //左边距
11 private int canvasLeft;
12 ///<summary>
13 /// 左边距
14 ///</summary>
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     ///<summary>
2 /// Bindingwithxaml.xaml 的交互逻辑
3 ///</summary>
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 ///<summary>
46 /// CLength.PropertyChanged事件绑定方法
47 ///</summary>
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 ///<summary>
56 /// myDispatcherTimer.Tick事件绑定方法
57 ///</summary>
58 void myDispatcherTimer_Tick(object sender, EventArgs e)
59 {
60 //设置CLength的CanvasLeft值
61 CLength.CanvasLeft += 10;
62 }
63
64 #endregion
65 }





原文地址:https://www.cnblogs.com/sirkevin/p/2256705.html