{"id":4923,"date":"2014-11-21T09:14:45","date_gmt":"2014-11-21T01:14:45","guid":{"rendered":"https:\/\/www.chenqing.work\/?p=4923"},"modified":"2023-09-14T18:58:03","modified_gmt":"2023-09-14T10:58:03","slug":"wpf-%e4%b8%ad%e7%9a%84-multibinding","status":"publish","type":"post","link":"https:\/\/www.chenqing.work\/?p=4923","title":{"rendered":"WPF \u4e2d\u7684 MultiBinding"},"content":{"rendered":"\n<p>\u5728 WPF \u4e2d\u8fdb\u884c\u6570\u636e\u7ed1\u5b9a\u65f6\uff0c\u6709\u65f6\u4f1a\u6709\u8fd9\u79cd\u9700\u6c42\uff1a\u5982 TextBlock \u7684 Text \u5c5e\u6027\uff0c\u53ef\u80fd\u5e0c\u671b\u7ed1\u5b9a\u5230\u4e24\u4e2a\u6216\u4ee5\u4e0a\u7684\u6570\u636e\u6e90\uff0c\u6700\u540e\u5f97\u5230\u7684 Text \u663e\u793a\u662f\u7531\u8fd9\u51e0\u4e2a\u6570\u636e\u6e90\u6309\u7167\u81ea\u5df1\u7684\u8bbe\u8ba1\u7ec4\u5408\u800c\u6210\uff0c\u5373 MultiBinding \u529f\u80fd\u3002\u8fd9\u91cc\u8bb0\u5f55\u4e0b\u4e24\u79cd\u7528\u6cd5\uff08\u7ed1\u5b9a\u7684\u6982\u5ff5\u8fd9\u91cc\u5c31\u4e0d\u5217\u4e86\uff0c\u76f4\u63a5\u4e0a\u5173\u952e\u4ee3\u7801\uff09\uff1a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1\u3001\u5728 XAML \u4e2d\u4f7f\u7528 MultiBinding + StringFormat<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;TextBlock&gt;\n    &lt;TextBlock.Text&gt;    \n        &lt;MultiBinding StringFormat=&quot;{}{0} + {1}&quot;&gt;\n            &lt;Binding Path=&quot;Name&quot; \/&gt;\n            &lt;Binding Path=&quot;ID&quot; \/&gt;\n        &lt;\/MultiBinding&gt;\n    &lt;\/TextBlock.Text&gt;\n&lt;\/TextBlock&gt;\n<\/pre><\/div>\n\n\n<p>\u8fd9\u91cc\u7684\u00a0StringFormat=&#8221;{}{0} + {1}&#8221;\u00a0\uff0c\u4e2d\u7684\u7b2c\u4e00\u4e2a {} \u662f\u6709\u5fc5\u8981\u5b58\u5728\u7684\u3002\u6700\u540e\u5f97\u5230\u7684\u663e\u793a\u4e5f\u5c31\u662f &lt;Name>+&lt;ID>\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2\u3001\u5728 XAML \u4e2d\u4f7f\u7528 MultiBinding + Converter<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n&lt;Windows.Resourses&gt;\n     &lt;my:NameConverter x:key=&quot;myNameConverter&quot;\/&gt;\n&lt;\/Windows.Resourses&gt;\n==============================================\n&lt;TextBlock&gt;\n   &lt;TextBlock.Text&gt;\n       &lt;MultiBinding Converter=&quot;{StaticResource myNameConverter}&quot;\n                     ConverterParameter=&quot;FormatLastFirst&quot;&gt;\n          &lt;Binding Path=&quot;FirstName&quot;\/&gt;\n          &lt;Binding Path=&quot;LastName&quot;\/&gt;\n       &lt;\/MultiBinding&gt;\n   &lt;\/TextBlock.Text&gt;\n&lt;\/TextBlock&gt;\n \n=======================================================\n=======================================================\n public class NameConverter : IMultiValueConverter\n{\n    public object Convert(object&#x5B;] values, Type targetType, object parameter, CultureInfo culture)\n    {\n        string name;\n \n        switch ((string)parameter)\n        {\n            case &quot;FormatLastFirst&quot;:\n                name = values&#x5B;1] + &quot;, &quot; + values&#x5B;0];\n                break;\n            case &quot;FormatNormal&quot;:\n                default:\n                name = values&#x5B;0] + &quot; &quot; + values&#x5B;1];\n                break;\n        }\n \n        return name;\n    }\n \n    public object&#x5B;] ConvertBack(object value, Type&#x5B;] targetTypes, object parameter, CultureInfo culture)\n    {\n        string&#x5B;] splitValues = ((string)value).Split(' ');\n        return splitValues;\n    }\n}\n<\/pre><\/div>\n\n\n<p>\u5728 Converter \u4e2d\u8fdb\u884c\u6570\u636e\u6e90\u7684\u7cbe\u7ec6\u7ec4\u5408\u3002\u53ef\u53c2\u89c1 <a rel=\"noreferrer noopener\" href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.windows.data.multibinding.converter%28v=vs.110%29.aspx\" target=\"_blank\">MSDN<\/a> \u4e0a\u7684\u4ecb\u7ecd\uff0c<a rel=\"noreferrer noopener\" href=\"http:\/\/www.codeproject.com\/Articles\/459958\/Bindable-Converter-Converter-Parameter-and-StringF\" target=\"_blank\">\u8fd9\u513f<\/a> \u662f\u53e6\u4e00\u4e2a\u4f8b\u5b50\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 WPF \u4e2d\u8fdb\u884c\u6570\u636e\u7ed1\u5b9a\u65f6\uff0c\u6709\u65f6\u4f1a\u6709\u8fd9\u79cd\u9700\u6c42\uff1a\u5982 TextBlock \u7684 Text \u5c5e\u6027\uff0c\u53ef\u80fd\u5e0c\u671b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[104,105],"class_list":["post-4923","post","type-post","status-publish","format-standard","hentry","category-code","tag-multibinding","tag-wpf"],"_links":{"self":[{"href":"https:\/\/www.chenqing.work\/index.php?rest_route=\/wp\/v2\/posts\/4923","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.chenqing.work\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chenqing.work\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chenqing.work\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chenqing.work\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4923"}],"version-history":[{"count":3,"href":"https:\/\/www.chenqing.work\/index.php?rest_route=\/wp\/v2\/posts\/4923\/revisions"}],"predecessor-version":[{"id":4926,"href":"https:\/\/www.chenqing.work\/index.php?rest_route=\/wp\/v2\/posts\/4923\/revisions\/4926"}],"wp:attachment":[{"href":"https:\/\/www.chenqing.work\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4923"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chenqing.work\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4923"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chenqing.work\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4923"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}