{"id":478,"date":"2019-03-27T15:52:15","date_gmt":"2019-03-27T06:52:15","guid":{"rendered":"http:\/\/marius.main.jp\/software\/blog\/?p=478"},"modified":"2020-03-25T17:58:35","modified_gmt":"2020-03-25T08:58:35","slug":"post-478","status":"publish","type":"post","link":"https:\/\/todosoft.net\/blog\/?p=478","title":{"rendered":"DataGrid\u5185\u306e\u30dc\u30bf\u30f3\u3092\u89aa\u306e\u30af\u30e9\u30b9\u3067\u53d7\u3051\u53d6\u308b"},"content":{"rendered":"\n<p>\u4e0b\u8a18\u30bd\u30fc\u30b9\u306e\u5834\u5408\u3001\u30dc\u30bf\u30f3\u3092\u62bc\u4e0b\u3059\u308b\u3068\u3001\u884c\u3054\u3068\u306e\u30af\u30e9\u30b9\u5185\u306e\u30a4\u30d9\u30f3\u30c8\u304c\u767a\u751f\u3057\u3066\u3057\u307e\u3046\u3002<\/p>\n\n\n\n<!--more-->\n\n\n\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=\"WpfApp6.MainWindow\"\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:local=\"clr-namespace:WpfApp6\"\n        xmlns:vm=\"clr-namespace:WpfApp6.ViewModel\"\n        mc:Ignorable=\"d\"\n        Title=\"MainWindow\" Height=\"200\" Width=\"300\">\n\n    &lt;Window.DataContext>\n        &lt;vm:MainViewModel \/>\n    &lt;\/Window.DataContext>\n\n    &lt;Grid>\n        &lt;DataGrid Margin=\"10\" ItemsSource=\"{Binding Path=ArrayData}\" AutoGenerateColumns=\"False\" SelectionMode=\"Single\" SelectionUnit=\"FullRow\">\n            &lt;DataGrid.CellStyle>\n                &lt;Style TargetType=\"DataGridCell\" BasedOn=\"{StaticResource {x:Type DataGridCell}}\">\n                    &lt;Setter Property=\"BorderThickness\" Value=\"0\" \/>\n                &lt;\/Style>\n            &lt;\/DataGrid.CellStyle>\n            &lt;DataGrid.Columns>\n                &lt;DataGridTextColumn Header=\"Index\" Binding=\"{Binding Path=Index}\" \/>\n\n                &lt;DataGridTemplateColumn Header=\"\u540d\u524d\">\n                    &lt;DataGridTemplateColumn.CellTemplate>\n                        &lt;DataTemplate>\n                            &lt;Button Content=\"{Binding Path=Name}\" Command=\"{Binding Path=ClickItem}\" \/>\n                        &lt;\/DataTemplate>\n                    &lt;\/DataGridTemplateColumn.CellTemplate>\n                &lt;\/DataGridTemplateColumn>\n                \n                &lt;DataGridTextColumn Header=\"\u5024\u6bb5\" Binding=\"{Binding Path=Price}\" \/>\n            &lt;\/DataGrid.Columns>\n        &lt;\/DataGrid>\n    &lt;\/Grid>\n&lt;\/Window><\/code><\/pre>\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.ObjectModel;\nusing System.ComponentModel;\nusing System.Runtime.CompilerServices;\nusing System.Windows;\nusing System.Windows.Input;\n\nnamespace WpfApp6.ViewModel\n{\n    public class MainViewModel : INotifyPropertyChangedBase\n    {\n        private ObservableCollection&lt;ItemPriceData> _ArrayData = new ObservableCollection&lt;ItemPriceData>();\n\n        public ObservableCollection&lt;ItemPriceData> ArrayData\n        {\n            get\n            {\n                return _ArrayData;\n            }\n            set\n            {\n                _ArrayData = value;\n                OnPropertyChanged();\n            }\n        }\n\n        private ICommand _ClickItem;\n        public ICommand ClickItem\n        {\n            get\n            {\n                return _ClickItem ?? (_ClickItem = new RelayCommand(() => { MessageBox.Show(\"MainViewModel\u5185\u306eClickItem\"); }));\n            }\n        }\n\n        public MainViewModel()\n        {\n            ArrayData.Add(new ItemPriceData(0, \"\u308a\u3093\u3054\", 150));\n            ArrayData.Add(new ItemPriceData(1, \"\u307f\u304b\u3093\", 200));\n            ArrayData.Add(new ItemPriceData(2, \"\u30d0\u30ca\u30ca\", 380));\n        }\n    }\n\n    public class ItemPriceData : INotifyPropertyChangedBase\n    {\n        private int _Index;\n\n        public int Index\n        {\n            get\n            {\n                return _Index;\n            }\n            set\n            {\n                _Index = value;\n                OnPropertyChanged();\n            }\n        }\n\n        private string _Name;\n\n        public string Name\n        {\n            get\n            {\n                return _Name;\n            }\n            set\n            {\n                _Name = value;\n                OnPropertyChanged();\n            }\n        }\n\n        private int _Price;\n\n        public int Price\n        {\n            get\n            {\n                return _Price;\n            }\n            set\n            {\n                _Price = value;\n                OnPropertyChanged();\n            }\n        }\n\n        public ItemPriceData(int intIndex, string strName, int intPrice)\n        {\n            Index = intIndex;\n            Name = strName;\n            Price = intPrice;\n        }\n\n        private ICommand _ClickItem;\n        public ICommand ClickItem\n        {\n            get\n            {\n                return _ClickItem ?? (_ClickItem = new RelayCommand(() => { MessageBox.Show(\"ItemPriceData\u5185\u306eClickItem\"); }));\n            }\n        }\n    }\n\n    public class INotifyPropertyChangedBase : INotifyPropertyChanged\n    {\n        public event PropertyChangedEventHandler PropertyChanged;\n\n        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)\n        {\n            if (PropertyChanged != null)\n                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));\n        }\n    }\n\n    public class RelayCommand : ICommand\n    {\n        private Action _action;\n\n        public RelayCommand(Action action)\n        {\n            _action = action;\n        }\n\n        public event EventHandler CanExecuteChanged;\n\n        public bool CanExecute(object parameter)\n        {\n            return _action != null;\n        }\n\n        public void Execute(object parameter)\n        {\n            _action?.Invoke();\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"372\" height=\"213\" src=\"http:\/\/marius.main.jp\/software\/blog\/wp-content\/uploads\/2019\/03\/cellclick1.png\" alt=\"\" class=\"wp-image-479\" srcset=\"https:\/\/todosoft.net\/blog\/wp-content\/uploads\/2019\/03\/cellclick1.png 372w, https:\/\/todosoft.net\/blog\/wp-content\/uploads\/2019\/03\/cellclick1-300x172.png 300w\" sizes=\"auto, (max-width: 372px) 100vw, 372px\" \/><\/figure>\n\n\n\n<p>\u30dc\u30bf\u30f3\u306eCommand\u306e\u30d0\u30a4\u30f3\u30c9\u3092\u4e0b\u8a18\u306e\u3088\u3046\u306b\u4fee\u6b63\u3059\u308b\u3053\u3068\u3067\u3001<br>\u89aa\u3067\u53d7\u3051\u53d6\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-luxe-blocks-syntaxhighlighter line-numbers language-csharp\"><code class=\"language-csharp\">&lt;Button Content=\"{Binding Path=Name}\" Command=\"{Binding Path=DataContext.ClickItem, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}\" \/><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"417\" height=\"208\" src=\"http:\/\/marius.main.jp\/software\/blog\/wp-content\/uploads\/2019\/03\/cellclick2.png\" alt=\"\" class=\"wp-image-480\" srcset=\"https:\/\/todosoft.net\/blog\/wp-content\/uploads\/2019\/03\/cellclick2.png 417w, https:\/\/todosoft.net\/blog\/wp-content\/uploads\/2019\/03\/cellclick2-300x150.png 300w\" sizes=\"auto, (max-width: 417px) 100vw, 417px\" \/><\/figure>\n\n\n\n<p>\u307e\u305f\u3001\u30dc\u30bf\u30f3\u306bCommandParameter\u3092\u8a2d\u5b9a\u3057\u3001RelayCommand\u3092\u5f15\u6570\u5bfe\u5fdc\u306b\u4fee\u6b63\u3059\u308b\u3053\u3068\u3067\u3001<br>\u3069\u306e\u30dc\u30bf\u30f3\u304c\u62bc\u3055\u308c\u305f\u304b\u3092\u89aa\u3067\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<\/p>\n\n\n\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=\"WpfApp6.MainWindow\"\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:local=\"clr-namespace:WpfApp6\"\n        xmlns:vm=\"clr-namespace:WpfApp6.ViewModel\"\n        mc:Ignorable=\"d\"\n        Title=\"MainWindow\" Height=\"200\" Width=\"300\">\n\n    &lt;Window.DataContext>\n        &lt;vm:MainViewModel \/>\n    &lt;\/Window.DataContext>\n\n    &lt;Grid>\n        &lt;DataGrid Margin=\"10\" ItemsSource=\"{Binding Path=ArrayData}\" AutoGenerateColumns=\"False\" SelectionMode=\"Single\" SelectionUnit=\"FullRow\">\n            &lt;DataGrid.CellStyle>\n                &lt;Style TargetType=\"DataGridCell\" BasedOn=\"{StaticResource {x:Type DataGridCell}}\">\n                    &lt;Setter Property=\"BorderThickness\" Value=\"0\" \/>\n                &lt;\/Style>\n            &lt;\/DataGrid.CellStyle>\n            &lt;DataGrid.Columns>\n                &lt;DataGridTextColumn Header=\"Index\" Binding=\"{Binding Path=Index}\" \/>\n\n                &lt;DataGridTemplateColumn Header=\"\u540d\u524d\">\n                    &lt;DataGridTemplateColumn.CellTemplate>\n                        &lt;DataTemplate>\n                            &lt;Button Content=\"{Binding Path=Name}\" Command=\"{Binding Path=DataContext.ClickItem, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}\" CommandParameter=\"{Binding Path=Index}\" \/>\n                        &lt;\/DataTemplate>\n                    &lt;\/DataGridTemplateColumn.CellTemplate>\n                &lt;\/DataGridTemplateColumn>\n                \n                &lt;DataGridTextColumn Header=\"\u5024\u6bb5\" Binding=\"{Binding Path=Price}\" \/>\n            &lt;\/DataGrid.Columns>\n        &lt;\/DataGrid>\n    &lt;\/Grid>\n&lt;\/Window><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">MainViewModel<\/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.ObjectModel;\nusing System.ComponentModel;\nusing System.Linq;\nusing System.Runtime.CompilerServices;\nusing System.Windows;\nusing System.Windows.Input;\n\nnamespace WpfApp6.ViewModel\n{\n    public class MainViewModel : INotifyPropertyChangedBase\n    {\n        private ObservableCollection&lt;ItemPriceData> _ArrayData = new ObservableCollection&lt;ItemPriceData>();\n\n        public ObservableCollection&lt;ItemPriceData> ArrayData\n        {\n            get\n            {\n                return _ArrayData;\n            }\n            set\n            {\n                _ArrayData = value;\n                OnPropertyChanged();\n            }\n        }\n\n        private ICommand _ClickItem;\n        public ICommand ClickItem\n        {\n            get\n            {\n                return _ClickItem ?? (_ClickItem = new RelayCommand((object param) => \n                {\n                    MessageBox.Show(_ArrayData.First(t => t.Index == (int)param).Name);\n                }));\n            }\n        }\n\n        public MainViewModel()\n        {\n            ArrayData.Add(new ItemPriceData(0, \"\u308a\u3093\u3054\", 150));\n            ArrayData.Add(new ItemPriceData(1, \"\u307f\u304b\u3093\", 200));\n            ArrayData.Add(new ItemPriceData(2, \"\u30d0\u30ca\u30ca\", 380));\n        }\n    }\n\n    public class ItemPriceData : INotifyPropertyChangedBase\n    {\n        private int _Index;\n\n        public int Index\n        {\n            get\n            {\n                return _Index;\n            }\n            set\n            {\n                _Index = value;\n                OnPropertyChanged();\n            }\n        }\n\n        private string _Name;\n\n        public string Name\n        {\n            get\n            {\n                return _Name;\n            }\n            set\n            {\n                _Name = value;\n                OnPropertyChanged();\n            }\n        }\n\n        private int _Price;\n\n        public int Price\n        {\n            get\n            {\n                return _Price;\n            }\n            set\n            {\n                _Price = value;\n                OnPropertyChanged();\n            }\n        }\n\n        public ItemPriceData(int intIndex, string strName, int intPrice)\n        {\n            Index = intIndex;\n            Name = strName;\n            Price = intPrice;\n        }\n\n        private ICommand _ClickItem;\n        public ICommand ClickItem\n        {\n            get\n            {\n                return _ClickItem ?? (_ClickItem = new RelayCommand((object param) => { MessageBox.Show(\"ItemPriceData\u5185\u306eClickItem\"); }));\n            }\n        }\n    }\n\n    public class INotifyPropertyChangedBase : INotifyPropertyChanged\n    {\n        public event PropertyChangedEventHandler PropertyChanged;\n\n        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)\n        {\n            if (PropertyChanged != null)\n                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));\n        }\n    }\n\n    public class RelayCommand : ICommand\n    {\n        private Action&lt;object> _action;\n\n        public RelayCommand(Action&lt;object> action)\n        {\n            _action = action;\n        }\n\n        public event EventHandler CanExecuteChanged;\n\n        public bool CanExecute(object parameter)\n        {\n            return _action != null;\n        }\n\n        public void Execute(object parameter)\n        {\n            _action?.Invoke(parameter);\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"367\" height=\"215\" src=\"http:\/\/marius.main.jp\/software\/blog\/wp-content\/uploads\/2019\/03\/cellclick3.png\" alt=\"\" class=\"wp-image-481\" srcset=\"https:\/\/todosoft.net\/blog\/wp-content\/uploads\/2019\/03\/cellclick3.png 367w, https:\/\/todosoft.net\/blog\/wp-content\/uploads\/2019\/03\/cellclick3-300x176.png 300w\" sizes=\"auto, (max-width: 367px) 100vw, 367px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u8a18\u30bd\u30fc\u30b9\u306e\u5834\u5408\u3001\u30dc\u30bf\u30f3\u3092\u62bc\u4e0b\u3059\u308b\u3068\u3001\u884c\u3054\u3068\u306e\u30af\u30e9\u30b9\u5185\u306e\u30a4\u30d9\u30f3\u30c8\u304c\u767a\u751f\u3057\u3066\u3057\u307e\u3046\u3002<\/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],"tags":[26,23],"class_list":["post-478","post","type-post","status-publish","format-standard","hentry","category-c","category-wpf","tag-datagrid","tag-wpf"],"_links":{"self":[{"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/478","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=478"}],"version-history":[{"count":3,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/478\/revisions"}],"predecessor-version":[{"id":714,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/478\/revisions\/714"}],"wp:attachment":[{"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}