{"id":764,"date":"2020-06-07T16:51:18","date_gmt":"2020-06-07T07:51:18","guid":{"rendered":"https:\/\/todosoft.net\/blog\/?p=764"},"modified":"2021-03-14T05:38:51","modified_gmt":"2021-03-13T20:38:51","slug":"post-764","status":"publish","type":"post","link":"https:\/\/todosoft.net\/blog\/?p=764","title":{"rendered":"ContextMenu\u306e\u4e2d\u8eab\u3092MVVM\u3067\u52d5\u7684\u306b\u4f5c\u6210\u3059\u308b"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">MainView.xaml<\/h2>\n\n\n\n<pre class=\"wp-block-luxe-blocks-syntaxhighlighter line-numbers language-csharp\"><code class=\"language-csharp\">&lt;Window x:Class=\"WpfApp15.View.MainView\"\n        xmlns=\"http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\/presentation\"\n        xmlns:x=\"http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\"\n        xmlns:d=\"http:\/\/schemas.microsoft.com\/expression\/blend\/2008\"\n        xmlns:mc=\"http:\/\/schemas.openxmlformats.org\/markup-compatibility\/2006\"\n        xmlns:i=\"http:\/\/schemas.microsoft.com\/xaml\/behaviors\"\n        xmlns:l=\"http:\/\/schemas.livet-mvvm.net\/2011\/wpf\"\n        xmlns:local=\"clr-namespace:WpfApp15.View\"\n        xmlns:vm=\"clr-namespace:WpfApp15.ViewModel\"\n        mc:Ignorable=\"d\"\n        Title=\"MainView\" Height=\"450\" Width=\"800\">\n\n    &lt;Window.DataContext>\n        &lt;vm:MainViewModel\/>\n    &lt;\/Window.DataContext>\n\n    &lt;i:Interaction.Triggers>\n        &lt;i:EventTrigger EventName =\"Loaded\">\n            &lt;l:LivetCallMethodAction MethodTarget=\"{Binding}\" MethodName=\"OnLoaded\" \/>\n        &lt;\/i:EventTrigger>\n    &lt;\/i:Interaction.Triggers>\n\n    &lt;Grid>\n        &lt;Button>\n            &lt;Button.ContextMenu>\n                &lt;ContextMenu>\n                    &lt;MenuItem Header=\"MenuParent\" ItemsSource=\"{Binding ArrayMenu1}\">\n                        &lt;MenuItem.ItemContainerStyle>\n                            &lt;Style TargetType=\"MenuItem\">\n                                &lt;Setter Property=\"Header\" Value=\"{Binding DisplayName}\"\/>\n                                &lt;Setter Property=\"IsChecked\" Value=\"{Binding Checked}\"\/>\n                                &lt;Setter Property=\"Command\" Value=\"{Binding}\"\/>\n                            &lt;\/Style>\n                        &lt;\/MenuItem.ItemContainerStyle>\n                    &lt;\/MenuItem>\n                &lt;\/ContextMenu>\n            &lt;\/Button.ContextMenu>\n        &lt;\/Button>\n    &lt;\/Grid>\n&lt;\/Window>\n<\/code><\/pre>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">MainViewModel.cs<\/h2>\n\n\n\n<pre class=\"wp-block-luxe-blocks-syntaxhighlighter line-numbers language-csharp\"><code class=\"language-csharp\">using System;\nusing System.Collections.Generic;\nusing System.Windows;\n\nnamespace WpfApp15.ViewModel\n{\n    public class MainViewModel : Livet.ViewModel\n    {\n        \/\/\/ &lt;summary>\n        \/\/\/ \u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u30e1\u30cb\u30e5\u30fc\n        \/\/\/ &lt;\/summary>\n        private List&lt;ContextMenuClass> _ArrayMenu1 = new List&lt;ContextMenuClass>();\n        public List&lt;ContextMenuClass> ArrayMenu1\n        {\n            get\n            {\n                return _ArrayMenu1;\n            }\n            set\n            {\n                _ArrayMenu1 = value;\n                RaisePropertyChanged();\n            }\n        }\n\n        \/\/\/ &lt;summary>\n        \/\/\/ \u8d77\u52d5\u6642\u51e6\u7406\n        \/\/\/ &lt;\/summary>\n        public void OnLoaded()\n        {\n            for(int intCount = 0; intCount &lt; 5; intCount++)\n            {\n                ContextMenuClass contextMenu = new ContextMenuClass(\"Menu\" + intCount, intCount == 0 ? true : false);\n                contextMenu.ExecuteEventHandler += (object sender, EventArgs e)=>\n                {\n                    foreach(ContextMenuClass Menu in ArrayMenu1)\n                    {\n                        Menu.Checked = Menu == sender ? true : false;\n                    }\n\n                    MessageBox.Show((sender as ContextMenuClass).DisplayName + \" Selected\");\n                };\n                ArrayMenu1.Add(contextMenu);\n            }\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">ContextMenuClass.cs<\/h2>\n\n\n\n<pre class=\"wp-block-luxe-blocks-syntaxhighlighter line-numbers language-csharp\"><code class=\"language-csharp\">using System;\nusing System.Windows.Input;\n\nnamespace WpfApp15.ViewModel\n{\n    \/\/\/ &lt;summary>\n    \/\/\/ \u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u30e1\u30cb\u30e5\u30fc\u30af\u30e9\u30b9\n    \/\/\/ &lt;\/summary>\n    public class ContextMenuClass : Livet.ViewModel, ICommand\n    {\n        public ContextMenuClass(string strDisplayName, bool bolChecked)\n        {\n            this.DisplayName = strDisplayName;\n            this.Checked = bolChecked;\n        }\n\n        private string _DisplayName = \"\";\n        public string DisplayName\n        {\n            get\n            {\n                return _DisplayName;\n            }\n            set\n            {\n                _DisplayName = value;\n                RaisePropertyChanged();\n            }\n        }\n\n        private bool _Checked = false;\n        public bool Checked\n        {\n            get\n            {\n                return _Checked;\n            }\n            set\n            {\n                _Checked = value;\n                RaisePropertyChanged();\n            }\n        }\n\n        public event EventHandler CanExecuteChanged;\n\n        public bool CanExecute(object parameter)\n        {\n            return true;\n        }\n\n        \/\/ \u9078\u629e\u6642\u30a4\u30d9\u30f3\u30c8\u30cf\u30f3\u30c9\u30e9\n        public event EventHandler ExecuteEventHandler;\n\n        public void Execute(object parameter)\n        {\n            if (ExecuteEventHandler != null)\n                ExecuteEventHandler(this, EventArgs.Empty);\n        }\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>MainView.xaml<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,20,15],"tags":[93],"class_list":["post-764","post","type-post","status-publish","format-standard","hentry","category-c","category-wpf","category-15","tag-93"],"_links":{"self":[{"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/764","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=764"}],"version-history":[{"count":2,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/764\/revisions"}],"predecessor-version":[{"id":766,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/764\/revisions\/766"}],"wp:attachment":[{"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}