Skip to content

Instantly share code, notes, and snippets.

@hilapon
Created January 14, 2015 08:31
Show Gist options
  • Save hilapon/3f6032f38206716d1c71 to your computer and use it in GitHub Desktop.
Save hilapon/3f6032f38206716d1c71 to your computer and use it in GitHub Desktop.
Person のコレクションを保持するViewModel
using Livet;
using Livet.Commands;
using MenuButtonApp2.Models;
using System.Collections.ObjectModel;
using System.Windows;
namespace MenuButtonApp2.ViewModels {
public class MainWindowViewModel : ViewModel {
public MainWindowViewModel() {
this.Persons = new ObservableCollection<Person>();
this.Persons.Add(new Person() {Id = 1, Name = "平本", Age = 17});
this.Persons.Add(new Person() {Id = 2, Name = "ファウラー", Age = 51});
this.Persons.Add(new Person() {Id = 3, Name = "ヘルスバーグ", Age = 54});
this.Persons.Add(new Person() {Id = 4, Name = "ペゾルト", Age = 61});
this.Persons.Add(new Person() {Id = 5, Name = "リヒター", Age = 54});
this.Persons.Add(new Person() {Id = 6, Name = "トーバルズ", Age = 45});
this.Persons.Add(new Person() {Id = 7, Name = "ストロヴストルップ", Age = 64});
this.Persons.Add(new Person() {Id = 8, Name = "カーニハン", Age = 73});
this.Persons.Add(new Person() {Id = 9, Name = "ベック", Age = 53});
}
#region Person変更通知プロパティ
private Person _Person;
public Person Person {
get { return _Person; }
set {
if (_Person == value) return;
_Person = value;
RaisePropertyChanged("Person");
}
}
#endregion
#region Persons変更通知プロパティ
private ObservableCollection<Person> _Persons;
public ObservableCollection<Person> Persons {
get { return _Persons; }
set {
if (_Persons == value) return;
_Persons = value;
RaisePropertyChanged("Persons");
}
}
#endregion
#region MessageCommand
private ViewModelCommand _MessageCommand;
public ViewModelCommand MessageCommand {
get {
if (_MessageCommand == null) {
_MessageCommand = new ViewModelCommand(Message);
}
return _MessageCommand;
}
}
public void Message() {
var message = new System.Text.StringBuilder();
message.AppendFormat("名前 = {0}", this.Person.Name);
message.AppendLine();
message.AppendFormat("年齢= {0}", this.Person.Age);
MessageBox.Show(message.ToString());
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment