<Window x:Class="WindowsApplication1.Window1"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
    Loaded
="OnLoad" Title="WindowsApplication1" Height="184" Width="280" xmlns:my="clr-namespace:System;assembly=mscorlib">
  
<Grid>
    
<Grid.ColumnDefinitions>
      
<ColumnDefinition Width="0.0481283422459894*" />
      
<ColumnDefinition Width="0.399344749210047*" />
      
<ColumnDefinition Width="0.552526908543964*" />
    
</Grid.ColumnDefinitions>
    
<Grid.RowDefinitions>
      
<RowDefinition Height="0*" />
      
<RowDefinition Height="*" />
    
</Grid.RowDefinitions>
    
<Button Click="btn1Click" Margin="10,10,10,0" Name="button1" Height="25" VerticalAlignment="Top" Grid.Row="1" Grid.Column="2" >Button</Button>
    
<Button Click="btn2Click" Margin="10,40,10,0" Name="button2" Height="25" VerticalAlignment="Top" Grid.Row="1" Grid.Column="2"  >Button</Button>
    
<TextBox Height="25" Margin="0,10,0,0" Name="textBox1" VerticalAlignment="Top" Grid.Row="1" Grid.Column="1"></TextBox>
    
<TextBox Text="{Binding Path=CityName}" Height="25" Margin="0,40,0,0" Name="textBox2" Grid.Row="1" VerticalAlignment="Top" Grid.Column="1"></TextBox>
    
<TextBox TextChanged="textBox3_TextChanged" VerticalAlignment="Top" Grid.Row="1" Margin="0,70,0,0" Name="textBox3" Grid.Column="1" Height="25"></TextBox>
    
<TextBox Grid.Row="1" Height="25" Margin="0,100,0,0" Name="textBox4" VerticalAlignment="Top" Grid.Column="1"></TextBox>
  
</Grid>
</Window>
  1using System;
  2using System.Collections.Generic;
  3using System.Text;
  4using System.Windows;
  5using System.Windows.Controls;
  6using System.Windows.Data;
  7using System.Windows.Documents;
  8using System.Windows.Input;
  9using System.Windows.Media;
 10using System.Windows.Media.Imaging;
 11using System.Windows.Shapes;
 12using System.ComponentModel;
 13using System.Reflection;
 14
 15
 16namespace WindowsApplication1
 17{
 18    /// <summary>
 19    /// Interaction logic for Window1.xaml
 20    /// </summary>
 21    /// 

 22
 23    public partial class Window1 : System.Windows.Window
 24    {
 25        Test city;
 26
 27        public Window1()
 28        {
 29            InitializeComponent();
 30        }

 31
 32        void OnLoad(object sender, RoutedEventArgs e)
 33        {
 34            CreateDataSource();
 35            this.textBox1.SetBinding(TextBox.TextProperty, "CityName");//代码设置绑定
 36            MyDataSourceGet();
 37            UseingMyBindClass();
 38        }

 39
 40        //使用MyBind类绑定控件和数据源。
 41        void UseingMyBindClass()
 42        {
 43            MyBind bind = new MyBind();
 44            bind.DataSource = city;
 45            bind.SetBinding(textBox4, TextBox.TextProperty, "CityName");//自定义绑定数据类使用。
 46        }

 47
 48        //使用委托获取数据源值改变。
 49        void MyDataSourceGet()
 50        {
 51            city.PropertyChanged += new PropertyChangedEventHandler(GetDataSourceChanged);
 52        }

 53
 54        //创建源。
 55        void CreateDataSource()
 56        {
 57            city = new Test();
 58            city.CityName = "hfidsahfjds";
 59            this.DataContext = city;
 60        }

 61
 62        //数据源属性值改变事件。
 63        void GetDataSourceChanged(object sender, PropertyChangedEventArgs e)
 64        {
 65            Type type = sender.GetType();
 66            MemberInfo member = type.GetMember(e.PropertyName)[0];
 67            object obj = type.InvokeMember(e.PropertyName, BindingFlags.GetProperty, null, sender, null);
 68            this.textBox3.Text = obj.ToString();
 69        }

 70
 71        //控件修改数据源值
 72        void textBox3_TextChanged(object sender, EventArgs e)
 73        {
 74            Type type = DataContext.GetType();
 75            type.GetProperty("CityName").SetValue(DataContext, textBox3.Text, null);
 76        }

 77
 78        void btn1Click(object sender, RoutedEventArgs e)
 79        {
 80            city.CityName = "123";//测试数据源修改值1
 81        }

 82
 83        void btn2Click(object sender, RoutedEventArgs e)
 84        {
 85            city.CityName = "456";//测试数据源修改值2
 86        }

 87
 88        /// <summary>
 89        /// 自定义DATABIND类
 90        /// </summary>

 91        class MyBind
 92        {
 93            object datasource;
 94            public object DataSource
 95            {
 96                get return datasource; }
 97                set
 98                {
 99                    datasource = value;
100                    INotifyPropertyChanged NotifyPropertyChanged = datasource as INotifyPropertyChanged;
101                    NotifyPropertyChanged.PropertyChanged += new PropertyChangedEventHandler(NotifyOnSourceUpdated);
102                }

103            }

104
105            object target;
106            string propertyName;
107            DependencyProperty dependencyProperty;
108            public void SetBinding(object Target, DependencyProperty dp, string PropertyName)
109            {
110                target = Target;
111                propertyName = PropertyName;
112                dependencyProperty = dp;
113                Control control = Target as Control;
114                control.LostFocus += new RoutedEventHandler(NotifyOnTargetUpdated);
115            }

116
117            void NotifyOnSourceUpdated(object sender, PropertyChangedEventArgs e)
118            {
119                Type type = sender.GetType();
120                target.GetType().GetProperty(dependencyProperty.Name).SetValue(target, type.GetProperty(propertyName).GetValue(sender, null), null);
121            }

122
123            void NotifyOnTargetUpdated(object sender, RoutedEventArgs e)
124            {
125                Type type = datasource.GetType().GetProperty(propertyName).PropertyType;
126                datasource.GetType().GetProperty(propertyName).SetValue(datasource, Convert.ChangeType(sender.GetType().GetProperty(dependencyProperty.Name).GetValue(sender, null), type), null);
127            }

128        }

129
130        /// <summary>
131        /// 继承INotifyPropertyChanged接口向BIND者发送数据源属性Update事件
132        /// </summary>

133        class Test : System.ComponentModel.INotifyPropertyChanged
134        {
135            string cityID;
136            public string CityID
137            {
138                get return cityID; }
139                set
140                {
141                    if (value != this.cityID)
142                    {
143                        this.cityID = value;
144                        NotifyPropertyChanged("CityID");
145                    }

146                }

147            }

148            string cityName;
149
150            public string CityName
151            {
152                get return cityName; }
153                set
154                {
155                    if (value != this.cityName)
156                    {
157                        this.cityName = value;
158                        NotifyPropertyChanged("CityName");
159                    }

160                }

161            }

162
163            INotifyPropertyChanged 成员
176        }

177
178    }

179}
posted on 2008-04-26 10:11  N Chen  阅读(346)  评论(1)    收藏  举报