WPF > Updateボタン押下時にDataGridに追加する > MVVM + ObservableCollection + ICommand


動作環境
Windows 8.1 Pro (64bit)
Microsoft Visual Studio 2017 Community

概要

  • ObservableCollection<T>型のmyItemを持つ
  • MVVM形式とする
  • ViewModelコンストラクタでmyItemにデータを生成
  • Updateボタン押下時にmyItemにデータを追加

ファイル

  • ViewModelBase.cs
  • MainWindow.xaml.cs
    • 初期実装のまま
  • MainWindow.xaml
    • 初期実装から変更
  • ViewModel.cs
    • 実装した

code

MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace _170630_t1800_MVVM_Icmd_DataTable
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}
MainWindow.xaml
<Window x:Class="_170630_t1800_MVVM_Icmd_DataTable.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:_170630_t1800_MVVM_Icmd_DataTable"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:ViewModel/>
    </Window.DataContext>
    <Grid>
        <StackPanel>
            <Button Content="Update" x:Name="uxUpdate" Width="100"
                    Command="{Binding UpdateCommand}"/>
            <DataGrid x:Name="dataGrid1" ItemsSource="{Binding myItem}"/>
        </StackPanel>
    </Grid>
</Window>
ViewModel.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
//
using System.Collections.ObjectModel;
using System.Windows.Input;
using System.Windows;

namespace _170630_t1800_MVVM_Icmd_DataTable
{
    class ViewModel : ViewModelBase
    {
        // コンストラクタ
        public ViewModel()
        {
            myItem = new ObservableCollection<TestItem>();
            myItem.Add(new TestItem { Id = 0, Name = "7of9", Age = 30, Gender = "Female" });
            myItem.Add(new TestItem { Id = 1, Name = "Janeway", Age = 50, Gender = "Female" });
            //
            UpdateCommand = CreateCommand(param => MyUpdateCommand());
        }

        public ObservableCollection<TestItem> myItem { get; set; }

        public ICommand UpdateCommand { get; private set; }

        public void MyUpdateCommand()
        {
            myItem.Add(new TestItem { Id = 2, Name = "Chakotay", Age = 45, Gender = "Male" });
            MessageBox.Show("Updated");
        }

        public class TestItem
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
            public string Gender { get; set; }
        }
    }
}

結果

初期状態

Updateボタン押下後