{"id":1609,"date":"2023-06-08T10:13:00","date_gmt":"2023-06-08T02:13:00","guid":{"rendered":"https:\/\/sanlangcode.com\/?p=1609"},"modified":"2023-06-08T10:13:00","modified_gmt":"2023-06-08T02:13:00","slug":"wpf%e4%b8%ad%e7%9a%84binding%e9%93%be","status":"publish","type":"post","link":"https:\/\/sanlangcode.com\/index.php\/2023\/06\/08\/wpf%e4%b8%ad%e7%9a%84binding%e9%93%be\/","title":{"rendered":"WPF\u4e2d\u7684binding\u94fe"},"content":{"rendered":"\n<p>\u5728WPF\uff08Windows Presentation Foundation\uff09\u4e2d\uff0cBinding\u94fe\u662f\u6307\u591a\u4e2a\u6570\u636e\u7ed1\u5b9a\u8868\u8fbe\u5f0f\uff08Binding Expression\uff09\u901a\u8fc7\u5c42\u5c42\u5d4c\u5957\u5f62\u6210\u7684\u94fe\u5f0f\u7ed3\u6784\u3002\u8fd9\u79cd\u94fe\u5f0f\u7ed3\u6784\u5141\u8bb8\u5c06\u591a\u4e2a\u5c5e\u6027\u6216\u5bf9\u8c61\u7684\u503c\u8fdb\u884c\u7ed1\u5b9a\uff0c\u4ee5\u5b9e\u73b0\u6570\u636e\u7684\u81ea\u52a8\u540c\u6b65\u548c\u66f4\u65b0\u3002<\/p>\n\n\n\n<p>Binding\u94fe\u7684\u7ec4\u6210\u90e8\u5206\u5305\u62ec\u6e90\u5bf9\u8c61\u3001\u76ee\u6807\u5bf9\u8c61\u4ee5\u53ca\u4e2d\u95f4\u7684\u7ed1\u5b9a\u8def\u5f84\u3002\u6e90\u5bf9\u8c61\u662f\u6570\u636e\u7684\u63d0\u4f9b\u8005\uff0c\u901a\u5e38\u662f ViewModel \u6216\u6570\u636e\u6a21\u578b\u5bf9\u8c61\u3002\u76ee\u6807\u5bf9\u8c61\u662f\u6570\u636e\u7684\u63a5\u6536\u8005\uff0c\u53ef\u4ee5\u662f WPF \u63a7\u4ef6\u6216\u5176\u4ed6 UI \u5143\u7d20\u3002\u7ed1\u5b9a\u8def\u5f84\u5b9a\u4e49\u4e86\u6e90\u5bf9\u8c61\u548c\u76ee\u6807\u5bf9\u8c61\u4e4b\u95f4\u7684\u5173\u8054\u5173\u7cfb\uff0c\u901a\u8fc7\u6307\u5b9a\u5c5e\u6027\u7684\u540d\u79f0\u6765\u63cf\u8ff0\u8def\u5f84\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u5c06\u7ed1\u5b9a\u8868\u8fbe\u5f0f\u5d4c\u5957\u5728\u4e00\u8d77\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u590d\u6742\u7684 Binding \u94fe\u3002\u6bcf\u4e2a\u7ed1\u5b9a\u8868\u8fbe\u5f0f\u90fd\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e <code>Path<\/code> \u5c5e\u6027\u6765\u6307\u5b9a\u8981\u7ed1\u5b9a\u7684\u5c5e\u6027\u6216\u8def\u5f84\uff0c\u540c\u65f6\u8fd8\u53ef\u4ee5\u8bbe\u7f6e\u5176\u4ed6\u5c5e\u6027\u6765\u5b9a\u4e49\u6570\u636e\u8f6c\u6362\u3001\u683c\u5f0f\u5316\u3001\u9a8c\u8bc1\u7b49\u884c\u4e3a\u3002<\/p>\n\n\n\n<p>\u5f53\u67d0\u4e2a\u5c5e\u6027\u5728 Binding \u94fe\u4e2d\u7684\u4efb\u4f55\u4e00\u7ea7\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u6574\u4e2a\u94fe\u4e2d\u7684\u7ed1\u5b9a\u8868\u8fbe\u5f0f\u90fd\u4f1a\u89e6\u53d1\u66f4\u65b0\uff0c\u786e\u4fdd\u6570\u636e\u7684\u540c\u6b65\u3002\u8fd9\u610f\u5473\u7740\u5982\u679c\u6e90\u5bf9\u8c61\u7684\u67d0\u4e2a\u5c5e\u6027\u53d1\u751f\u53d8\u5316\uff0c\u6240\u6709\u7ed1\u5b9a\u5230\u8be5\u5c5e\u6027\u7684\u76ee\u6807\u5bf9\u8c61\u90fd\u4f1a\u76f8\u5e94\u66f4\u65b0\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528 Binding \u94fe\u53ef\u4ee5\u6784\u5efa\u7075\u6d3b\u7684\u6570\u636e\u7ed1\u5b9a\u5173\u7cfb\uff0c\u4f7f\u6570\u636e\u5728\u4e0d\u540c\u7684\u5bf9\u8c61\u4e4b\u95f4\u4fdd\u6301\u540c\u6b65\u3002\u8fd9\u5bf9\u4e8e\u5b9e\u73b0 MVVM\uff08Model-View-ViewModel\uff09\u67b6\u6784\u6216\u5176\u4ed6\u9700\u8981\u6570\u636e\u9a71\u52a8\u7684\u5e94\u7528\u7a0b\u5e8f\u6a21\u5f0f\u975e\u5e38\u6709\u7528\u3002\u901a\u8fc7\u6b63\u786e\u914d\u7f6e Binding \u94fe\uff0c\u53ef\u4ee5\u5b9e\u73b0\u6570\u636e\u7684\u81ea\u52a8\u66f4\u65b0\u3001\u53cc\u5411\u7ed1\u5b9a\u4ee5\u53ca\u590d\u6742\u7684\u6570\u636e\u8f6c\u6362\u548c\u9a8c\u8bc1\u903b\u8f91\u3002<\/p>\n\n\n\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728\u521b\u5efa Binding \u94fe\u65f6\u9700\u8981\u6ce8\u610f\u6f5c\u5728\u7684\u5faa\u73af\u5f15\u7528\u95ee\u9898\uff0c\u4ee5\u907f\u514d\u51fa\u73b0\u65e0\u9650\u5faa\u73af\u66f4\u65b0\u7684\u60c5\u51b5\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e <code>Mode<\/code> \u5c5e\u6027\u4e3a <code>OneTime<\/code> \u6216 <code>OneWay<\/code> \u6765\u9632\u6b62\u5faa\u73af\u5f15\u7528\uff0c\u6216\u8005\u4f7f\u7528\u5176\u4ed6\u673a\u5236\u6765\u5904\u7406\u5faa\u73af\u5f15\u7528\u7684\u60c5\u51b5\u3002<\/p>\n\n\n\n<p>\u603b\u800c\u8a00\u4e4b\uff0cWPF \u4e2d\u7684 Binding \u94fe\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u673a\u5236\uff0c\u53ef\u4ee5\u5c06\u591a\u4e2a\u5c5e\u6027\u6216\u5bf9\u8c61\u7684\u503c\u8fdb\u884c\u7ed1\u5b9a\uff0c\u5b9e\u73b0\u6570\u636e\u7684\u81ea\u52a8\u540c\u6b65\u548c\u66f4\u65b0\u3002\u901a\u8fc7\u7075\u6d3b\u914d\u7f6e Binding \u94fe\uff0c\u53ef\u4ee5\u5b9e\u73b0\u590d\u6742\u7684\u6570\u636e\u7ed1\u5b9a\u5173\u7cfb\uff0c\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u7075\u6d3b\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n\n\n\n<p><br>\u5f53\u6211\u4eec\u5728WPF\u4e2d\u4f7f\u7528Binding\u94fe\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7XAML\u6216\u4ee3\u7801\u6765\u521b\u5efa\u548c\u914d\u7f6e\u7ed1\u5b9a\u5173\u7cfb\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728\u4ee3\u7801\u4e2d\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Binding\u94fe\uff1a<\/p>\n\n\n\n<p>\u9996\u5148\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u7c7b\uff1a<code>Person<\/code> \u548c <code>Address<\/code>\uff0c\u5176\u4e2d <code>Person<\/code> \u5305\u542b\u4e00\u4e2a <code>Name<\/code> \u5c5e\u6027\uff0c<code>Address<\/code> \u5305\u542b\u4e00\u4e2a <code>City<\/code> \u5c5e\u6027\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">csharpCopy code<code>public class Person\n{\n    public string Name { get; set; }\n    public Address Address { get; set; }\n\n    public Person()\n    {\n        Address = new Address();\n    }\n}\n\npublic class Address\n{\n    public string City { get; set; }\n}\n<\/code><\/pre>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2aWPF\u7a97\u53e3\uff0c\u5e76\u5728\u4ee3\u7801\u4e2d\u521b\u5efaBinding\u94fe\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">csharpCopy code<code>public partial class MainWindow : Window\n{\n    public MainWindow()\n    {\n        InitializeComponent();\n\n        \/\/ \u521b\u5efaPerson\u5bf9\u8c61\u548cAddress\u5bf9\u8c61\n        var person = new Person { Name = \"John Doe\" };\n        person.Address.City = \"New York\";\n\n        \/\/ \u521b\u5efaBinding\u94fe\n        var nameBinding = new Binding(\"Name\");\n        nameBinding.Source = person;\n\n        var cityBinding = new Binding(\"Address.City\");\n        cityBinding.Source = person;\n\n        \/\/ \u5c06Binding\u5e94\u7528\u5230\u63a7\u4ef6\n        nameLabel.SetBinding(TextBlock.TextProperty, nameBinding);\n        cityLabel.SetBinding(TextBlock.TextProperty, cityBinding);\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86 <code>Person<\/code> \u548c <code>Address<\/code> \u5bf9\u8c61\uff0c\u5e76\u4e3a\u5b83\u4eec\u7684\u5c5e\u6027\u8d4b\u503c\u3002\u7136\u540e\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2aBinding\u5bf9\u8c61\uff0c\u5206\u522b\u5bf9\u5e94 <code>Name<\/code> \u548c <code>Address.City<\/code> \u5c5e\u6027\u3002\u6211\u4eec\u5c06 <code>person<\/code> \u5bf9\u8c61\u8bbe\u7f6e\u4e3aBinding\u7684\u6e90\u5bf9\u8c61\uff0c\u5e76\u5c06Binding\u5e94\u7528\u5230\u4e24\u4e2aTextBlock\u63a7\u4ef6\u4e0a\uff0c\u5206\u522b\u663e\u793a <code>Name<\/code> \u548c <code>Address.City<\/code> \u7684\u503c\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684Binding\u94fe\uff0c\u5f53 <code>person<\/code> \u5bf9\u8c61\u7684\u5c5e\u6027\u503c\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u5bf9\u5e94\u7684TextBlock\u63a7\u4ef6\u7684\u5185\u5bb9\u4e5f\u4f1a\u76f8\u5e94\u66f4\u65b0\u3002<\/p>\n\n\n\n<p>\u8bf7\u6ce8\u610f\uff0c\u4e0a\u8ff0\u793a\u4f8b\u4e2d\u7684\u4ee3\u7801\u4ec5\u6f14\u793a\u4e86Binding\u94fe\u7684\u57fa\u672c\u7528\u6cd5\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u8fdb\u884c\u66f4\u590d\u6742\u7684\u914d\u7f6e\uff0c\u5982\u6dfb\u52a0\u6570\u636e\u8f6c\u6362\u5668\u3001\u6307\u5b9a\u66f4\u65b0\u6a21\u5f0f\u7b49\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u66f4\u590d\u6742\u7684\u4ee3\u7801\u4f8b\u5b50\uff0c\u5c55\u793a\u4e86\u5728WPF\u4e2d\u521b\u5efa\u66f4\u590d\u6742\u7684Binding\u94fe\u548c\u4f7f\u7528\u6570\u636e\u8f6c\u6362\u5668\u7684\u793a\u4f8b\uff1a<\/p>\n\n\n\n<p>\u9996\u5148\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e09\u4e2a\u7c7b\uff1a<code>Person<\/code>\u3001<code>Address<\/code>\u548c<code>Country<\/code>\uff0c\u5176\u4e2d <code>Person<\/code> \u5305\u542b\u4e00\u4e2a <code>Name<\/code> \u5c5e\u6027\uff0c<code>Address<\/code> \u5305\u542b\u4e00\u4e2a <code>City<\/code> \u5c5e\u6027\u548c\u4e00\u4e2a <code>Country<\/code> \u5bf9\u8c61\uff0c<code>Country<\/code> \u5305\u542b\u4e00\u4e2a <code>Name<\/code> \u5c5e\u6027\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">csharpCopy code<code>public class Person\n{\n    public string Name { get; set; }\n    public Address Address { get; set; }\n\n    public Person()\n    {\n        Address = new Address();\n    }\n}\n\npublic class Address\n{\n    public string City { get; set; }\n    public Country Country { get; set; }\n\n    public Address()\n    {\n        Country = new Country();\n    }\n}\n\npublic class Country\n{\n    public string Name { get; set; }\n}\n<\/code><\/pre>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2aWPF\u7a97\u53e3\uff0c\u5e76\u5728\u4ee3\u7801\u4e2d\u521b\u5efa\u66f4\u590d\u6742\u7684Binding\u94fe\uff0c\u5e76\u4f7f\u7528\u6570\u636e\u8f6c\u6362\u5668\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">csharpCopy code<code>public partial class MainWindow : Window\n{\n    public MainWindow()\n    {\n        InitializeComponent();\n\n        \/\/ \u521b\u5efaPerson\u5bf9\u8c61\u3001Address\u5bf9\u8c61\u548cCountry\u5bf9\u8c61\n        var person = new Person { Name = \"John Doe\" };\n        person.Address.City = \"New York\";\n        person.Address.Country.Name = \"USA\";\n\n        \/\/ \u521b\u5efaBinding\u94fe\n        var nameBinding = new Binding(\"Name\");\n        nameBinding.Source = person;\n\n        var cityBinding = new Binding(\"Address.City\");\n        cityBinding.Source = person;\n\n        var countryBinding = new Binding(\"Address.Country.Name\");\n        countryBinding.Source = person;\n\n        \/\/ \u521b\u5efa\u6570\u636e\u8f6c\u6362\u5668\n        var countryConverter = new CountryConverter();\n\n        \/\/ \u8bbe\u7f6e\u6570\u636e\u8f6c\u6362\u5668\u548c\u53c2\u6570\n        countryBinding.Converter = countryConverter;\n        countryBinding.ConverterParameter = \" (converted)\";\n\n        \/\/ \u5c06Binding\u5e94\u7528\u5230\u63a7\u4ef6\n        nameLabel.SetBinding(TextBlock.TextProperty, nameBinding);\n        cityLabel.SetBinding(TextBlock.TextProperty, cityBinding);\n        countryLabel.SetBinding(TextBlock.TextProperty, countryBinding);\n    }\n}\n\n\/\/ \u81ea\u5b9a\u4e49\u6570\u636e\u8f6c\u6362\u5668\npublic class CountryConverter : IValueConverter\n{\n    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)\n    {\n        string countryName = value.ToString();\n        string convertedCountryName = countryName + parameter.ToString();\n        return convertedCountryName;\n    }\n\n    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)\n    {\n        throw new NotImplementedException();\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86 <code>Person<\/code>\u3001<code>Address<\/code> \u548c <code>Country<\/code> \u5bf9\u8c61\uff0c\u5e76\u4e3a\u5b83\u4eec\u7684\u5c5e\u6027\u8d4b\u503c\u3002\u7136\u540e\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e09\u4e2aBinding\u5bf9\u8c61\uff0c\u5206\u522b\u5bf9\u5e94 <code>Name<\/code>\u3001<code>Address.City<\/code> \u548c <code>Address.Country.Name<\/code> \u5c5e\u6027\u3002\u6211\u4eec\u5c06 <code>person<\/code> \u5bf9\u8c61\u8bbe\u7f6e\u4e3aBinding\u7684\u6e90\u5bf9\u8c61\uff0c\u5e76\u5c06Binding\u5e94\u7528\u5230\u76f8\u5e94\u7684TextBlock\u63a7\u4ef6\u4e0a\u3002<\/p>\n\n\n\n<p>\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a <code>CountryConverter<\/code> \u7684\u81ea\u5b9a\u4e49\u6570\u636e\u8f6c\u6362\u5668\u3002\u8be5\u8f6c\u6362\u5668\u7528\u4e8e\u5c06\u56fd\u5bb6\u540d\u79f0\u8f6c\u6362\u4e3a\u683c\u5f0f\u5316\u540e\u7684\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u8f6c\u6362\u540e\u7684\u503c\u663e\u793a\u5728 <code>countryLabel<\/code> \u7684TextBlock\u63a7\u4ef6\u4e0a\u3002\u6211\u4eec\u901a\u8fc7\u8bbe\u7f6e <code>countryBinding<\/code> \u7684 <code>Converter<\/code> \u5c5e\u6027\u548c <code>ConverterParameter<\/code> \u5c5e\u6027\u6765\u4f7f\u7528\u6570\u636e\u8f6c\u6362\u5668\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u66f4\u590d\u6742\u7684Binding\u94fe\uff0c\u5e76\u4f7f\u7528\u4e86\u81ea\u5b9a\u4e49\u7684\u6570\u636e\u8f6c\u6362\u5668\u6765\u4fee\u6539\u7ed1\u5b9a\u503c\u7684\u663e\u793a\u3002\u8fd9\u6837\u53ef\u4ee5\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7075\u6d3b\u5904\u7406\u6570\u636e\uff0c\u5e76\u5728UI\u4e0a\u8fdb\u884c\u9002\u5f53\u7684\u683c\u5f0f\u5316\u548c\u663e\u793a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728WPF\uff08Windows Pre&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-1609","post","type-post","status-publish","format-standard","hentry","category-4"],"_links":{"self":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/posts\/1609","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/comments?post=1609"}],"version-history":[{"count":0,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/posts\/1609\/revisions"}],"wp:attachment":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/media?parent=1609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/categories?post=1609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/tags?post=1609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}