{"id":875,"date":"2021-09-15T22:25:12","date_gmt":"2021-09-15T14:25:12","guid":{"rendered":"http:\/\/www.wayln.com\/?p=875"},"modified":"2021-09-15T22:25:12","modified_gmt":"2021-09-15T14:25:12","slug":"%e4%be%9d%e8%b5%96%e9%a1%b9%e5%b1%9e%e6%80%a7","status":"publish","type":"post","link":"https:\/\/www.wayln.com\/?p=875","title":{"rendered":"\u4f9d\u8d56\u9879\u5c5e\u6027"},"content":{"rendered":"<p>\u4f9d\u8d56\u9879\u5c5e\u6027\u4e13\u95e8\u9488\u5bf9WPF\u521b\u5efa\uff0c\u5f53\u5e0c\u671b\u672c\u4e0d\u652f\u6301\u6570\u636e\u7ed1\u5b9a\u3001\u52a8\u753b\u6216\u5176\u4ed6WPF\u529f\u80fd\u7684\u90e8\u5206\u4ee3\u7801\u6dfb\u52a0\u8fd9\u4e9b\u529f\u80fd\u65f6\uff0c\u4e5f\u8bb8\u521b\u5efa\u4f9d\u8d56\u9879\u5c5e\u6027\u3002<br \/>\n\u53ea\u80fd\u4e3a\u4f9d\u8d56\u5bf9\u8c61\uff08\u7ee7\u627f\u81eaDependencyObject\u7684\u7c7b\uff09\u6dfb\u52a0\u4f9d\u8d56\u9879\u5c5e\u6027\uff0c\u5e78\u8fd0\u7684\u662f\uff0cWPF\u57fa\u7840\u7ed3\u6784\u7684\u5173\u952e\u90e8\u5206\u4e2d\u7684\u5927\u90e8\u5206\u90fd\u95f4\u63a5\u7ee7\u627f\u81eaDependencyObject\u7c7b\uff0c\u6700\u660e\u663e\u7684\u4f8b\u5b50\u5c31\u662f\u5143\u7d20\u3002<\/p>\n<ol>\n<li>\u5b9a\u4e49\u4f9d\u8d56\u9879\u5c5e\u6027<br \/>\n\u4ed6\u662fDeoendencyProperty\u7c7b\u7684\u5b9e\u4f8b\u3002\u5c5e\u6027\u4fe1\u606f\u5e94\u8be5\u59cb\u7ec8\u4fdd\u6301\u53ef\u7528\uff0c\u751a\u81f3\u53ef\u80fd\u9700\u8981\u5728\u591a\u4e2a\u7c7b\u4e4b\u95f4\u5171\u4eab\u8fd9\u4e9b\u4fe1\u606f\uff08\u5728WPF\u5143\u7d20\u4e2d\u8fd9\u662f\u5341\u5206\u666e\u904d\u7684\uff09\u3002\u56e0\u6b64\uff0c\u5fc5\u987b\u5c06DependencyProperty\u5bf9\u8c61\u5b9a\u4e49\u4e3a\u4e0e\u5176\u76f8\u5173\u8054\u7684\u7c7b\u7684\u9759\u6001\u5b57\u6bb5\uff0c\u4f8b\u5982\uff1a<\/li>\n<\/ol>\n<pre><code class=\"language-CSharp line-numbers\">public cliass FrameworkElement:UIElement,...\n{\n    public static readonly DependencyProperty MarginProperty;\n    ...\n}\n<\/code><\/pre>\n<p>\u6309\u7167\u7ea6\u5b9a\uff0c\u5b9a\u4e49\u4f9d\u8d56\u9879\u5c5e\u6027\u7684\u5b57\u6bb5\u7684\u540d\u79f0\u662f\u5728\u666e\u901a\u5c5e\u6027\u7684\u672b\u5c3e\u52a0\u4e0a\u5355\u8bcd\u201cProperty&#8221;,\u6839\u636e\u8fd9\u79cd\u547d\u540d\u65b9\u5f0f\uff0c\u53ef\u4ee5\u4ece\u5b9e\u9645\u5c5e\u6027\u7684\u540d\u79f0\u4e2d\u533a\u5206\u51fa\u4f9d\u8d56\u9879\u5c5e\u6027\u7684\u5b9a\u4e49\u3002\u5b57\u6bb5\u7684\u5b9a\u4e49\u4f7f\u7528\u4e86readonly\u5173\u952e\u5b57\uff0c\u8fd9\u610f\u5473\u7740\uff0c\u53ea\u80fd\u5728FrameworkElement\u7c7b\u7684\u9759\u6001\u6784\u9020\u51fd\u6570\u4e2d\u5bf9\u9f50\u8fdb\u884c\u8bbe\u7f6e<\/p>\n<ol start=\"2\">\n<li>\u6ce8\u518c\u4f9d\u8d56\u9879\u5c5e\u6027<br \/>\n\u8fd9\u4e00\u6b65\u9700\u8981\u5728\u4efb\u4f55\u4f7f\u7528\u5c5e\u6027\u7684\u4ee3\u7801\u4e4b\u524d\u5b8c\u6210\uff0c\u56e0\u6b64\u5fc5\u987b\u5728\u4e8e\u5176\u5173\u8054\u7684\u7c7b\u7684\u9759\u6001\u6784\u9020\u51fd\u6570\u4e2d\u8fdb\u884c\u3002<br \/>\nWPF\u53bb\u62a5DependencyProperty\u5bf9\u8c61\u4e0d\u80fd\u88ab\u76f4\u63a5\u5b9e\u4f8b\u5316\uff0c\u56e0\u4e3aDependencyProperty\u7c7b\u6ca1\u6709\u5171\u6709\u7684\u6784\u9020\u51fd\u6570\uff0c\u76f8\u53cd\uff0c\u53ea\u80fd\u4f7f\u7528DepencyProperty.Register()\u65b9\u6cd5\u521b\u5efaDependencyProperty\u5b9e\u4f8b\u3002WPF\u8fd8\u786e\u4fdd\u5728\u521b\u5efaDependencyProperty\u5bf9\u8c61\u540e\u4e0d\u80fd\u6539\u53d8\u8be5\u5bf9\u8c61\uff0c\u56e0\u4e3a\u6240\u6709DependencyProperty\u6210\u5458\u90fd\u662f\u53ea\u8bfb\u7684\uff0c\u4ed6\u4eec\u7684\u503c\u5fc5\u987b\u4f5c\u4e3aRegister()\u65b9\u6cd5\u7684\u53c2\u6570\u6765\u63d0\u4f9b\u3002<\/li>\n<\/ol>\n<pre><code class=\"language-CSharp line-numbers\">static FrameworkElement()\n{\n    FrameworkProperyMetadata metadata=new FrameworkPropertyMetadata(new Thickness(),FrameworkPropertyMeadataPption.AffectsMeasure);\n    MarginProperty=DependencyProperty.Register(\"Margin\",typeof(Thickness),typeof(FrameworkElement),metadata,new ValidateValuCallback(FrameworkElement.IsMarginValid));\n}\n<\/code><\/pre>\n<p>\u6ce8\u518c\u4f9d\u8d56\u9879\u5c5e\u6027\u9700\u8981\u7ecf\u5386\u4e24\u4e2a\u6b65\u9aa4\u3002\u9996\u5148\u521b\u5efaFrameworkPropertyMetadata\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u6307\u793a\u5e0c\u671b\u901a\u8fc7\u4f9d\u8d56\u9879\u5c5e\u6027\u4f7f\u7528\u4ec0\u4e48\u670d\u52a1\uff08\u5982\u652f\u6301\u6570\u636e\u7ed1\u5b9a\u3001\u52a8\u753b\u4ee5\u53ca\u65e5\u5fd7\uff09\u3002\u63a5\u4e0b\u6765\u901a\u8fc7\u8c03\u7528DependencyProperty.Register()\u9759\u6001\u65b9\u6cd5\u6ce8\u518c\u5c5e\u6027\u3002<br \/>\n3. \u6dfb\u52a0\u5c5e\u6027\u5305\u88c5\u5668<br \/>\npublic Thickness Margin<br \/>\n{<br \/>\nset{setValue(MarginProperty,value);}<br \/>\nget{return (Thickness)Getvalue(MarginProperty);}<br \/>\n}<\/p>\n<p><strong>\u6709\u4e00\u4e2a\u7ec6\u8282\uff1a\u4f9d\u8d56\u9879\u5c5e\u6027\u5c0a\u5f90\u4e25\u683c\u7684\u4f18\u5148\u89c4\u5219\u6765\u786e\u5b9a\u4ed6\u4eec\u7684\u5f53\u524d\u503c\uff0c\u5373\u4f7f\u6ca1\u6709\u76f4\u63a5\u8bbe\u7f6e\u4f9d\u8d56\u9879\u5c5e\u6027\uff0c\u4ed6\u4e5f\u53ef\u80fd\u5df2\u7ecf\u6709\u4e86\u6570\u503c\uff0c\u8be5\u6570\u503c\u53ef\u80fd\u7531\u6570\u636e\u7ed1\u5b9a\u3001\u6837\u5f0f\u6216\u52a8\u753b\u63d0\u4f9b\u7684\uff0c\u4e5f\u53ef\u80fd\u662f\u901a\u8fc7\u5143\u7d20\u6811\u7ee7\u627f\u6765\u7684\u3002\u4e0d\u8fc7\uff0c\u53ea\u8981\u76f4\u63a5\u8bbe\u7f6e\u4e86\u5c5e\u6027\u503c\uff0c\u8bbe\u7f6e\u7684\u5c5e\u6027\u503c\u5c31\u4f1a\u8986\u76d6\u6240\u6709\u5176\u4ed6\u5f71\u54cd<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f9d\u8d56\u9879\u5c5e\u6027\u4e13\u95e8\u9488\u5bf9WPF\u521b\u5efa\uff0c\u5f53\u5e0c\u671b\u672c\u4e0d\u652f\u6301\u6570\u636e\u7ed1\u5b9a\u3001\u52a8\u753b\u6216\u5176\u4ed6WPF\u529f\u80fd\u7684\u90e8\u5206\u4ee3\u7801\u6dfb\u52a0\u8fd9\u4e9b\u529f\u80fd\u65f6\uff0c\u4e5f\u8bb8\u521b\u5efa\u4f9d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,35,36,2],"tags":[],"class_list":["post-875","post","type-post","status-publish","format-standard","hentry","category-c","category-wpf","category-2"],"_links":{"self":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/875","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=875"}],"version-history":[{"count":1,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/875\/revisions"}],"predecessor-version":[{"id":876,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/875\/revisions\/876"}],"wp:attachment":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=875"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=875"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=875"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}