{"id":351,"date":"2021-03-14T18:34:36","date_gmt":"2021-03-14T18:34:36","guid":{"rendered":"http:\/\/robinluo.top\/?p=351"},"modified":"2021-03-14T18:34:36","modified_gmt":"2021-03-14T18:34:36","slug":"c-databinding","status":"publish","type":"post","link":"https:\/\/robinluo.top\/?p=351","title":{"rendered":"C# DataBinding"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>\nusing System;\nusing System.Collections.Generic;\nusing System.ComponentModel;\nusing System.Data;\nusing System.Drawing;\nusing System.Linq;\nusing System.Text;\nusing System.Windows.Forms;\nusing System.Runtime.CompilerServices;\n \nnamespace WindowsFormsApplication1\n{\n    public partial class Form2 : Form\n    {\n        public Form2()\n        {\n            InitializeComponent();\n        }\n \n        Test test;\n        private void Form2_Load(object sender, EventArgs e)\n        {\n            test=new Test();\n            label1.DataBindings.Add(\"Text\", test, \"Str\");\n            label2.DataBindings.Add(\"Text\", test, \"Str\");\n            label3.DataBindings.Add(\"Text\", test, \"Str\");\n            \n        }\n \n        private void textBox1_TextChanged(object sender, EventArgs e)\n        {\n            test.Str = textBox1.Text;\n        }\n \n    }\n \n    \/\/\u8981\u4f7f\u7528\u7ed1\u5b9a\u6570\u636e\u529f\u80fd\uff0c\u9700\u8981\u6a21\u578b\u652f\u6301INotifyPropertyChanged\u63a5\u53e3\n    public class Test : INotifyPropertyChanged\n    {\n        string _str;\n        public string Str\n        {\n            get\n            {\n                return _str;\n            }\n            set\n            {\n                _str = value;\n                FireStrChanged();\n            }\n        }\n \n        \/\/\u5fc5\u987b\u5b9e\u73b0INotifyPropertyChanged\u63a5\u53e3\u7684\u6b64\u4e8b\u4ef6\n        public event PropertyChangedEventHandler PropertyChanged;\n \n        \/\/\u8981\u5728.net4.0\u4f7f\u7528CallerMemberName\u7279\u6027\uff0c\u9700\u8981\u52a0\u4e0a\u540e\u9762\u4e00\u6bb5\u4ee3\u7801\n        public void FireStrChanged(&#91;CallerMemberName] string propertyName=\"\")\n        {\n            PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));\n        }\n \n    }\n}\n \nnamespace System.Runtime.CompilerServices\n{\n    &#91;AttributeUsage(AttributeTargets.Parameter, Inherited = false)]\n    public class CallerMemberNameAttribute : Attribute\n    {\n \n    }\n \n    &#91;AttributeUsage(AttributeTargets.Parameter, Inherited = false)]\n    public class CallerFilePathAttribute : Attribute\n    {\n \n    }\n \n    &#91;AttributeUsage(AttributeTargets.Parameter, Inherited = false)]\n    public class CallerLineNumberAttribute : Attribute\n    {\n \n    }\n}\r\n\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\r\n\u7248\u6743\u58f0\u660e\uff1a\u672c\u6587\u4e3aCSDN\u535a\u4e3b\u300cnaruto2017\u300d\u7684\u539f\u521b\u6587\u7ae0\uff0c\u9075\u5faaCC 4.0 BY-SA\u7248\u6743\u534f\u8bae\uff0c\u8f6c\u8f7d\u8bf7\u9644\u4e0a\u539f\u6587\u51fa\u5904\u94fe\u63a5\u53ca\u672c\u58f0\u660e\u3002\r\n\u539f\u6587\u94fe\u63a5\uff1ahttps:\/\/blog.csdn.net\/szy759590387\/article\/details\/89516163<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[50],"tags":[55],"_links":{"self":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/351"}],"collection":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=351"}],"version-history":[{"count":1,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/351\/revisions"}],"predecessor-version":[{"id":352,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/351\/revisions\/352"}],"wp:attachment":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}