Skip to content

Instantly share code, notes, and snippets.

@yoshiiz
Last active April 16, 2019 15:39
Show Gist options
  • Save yoshiiz/d7769ea537941fae5242186ae185318a to your computer and use it in GitHub Desktop.
Save yoshiiz/d7769ea537941fae5242186ae185318a to your computer and use it in GitHub Desktop.
[Visual C# 2017][WPF] WpfMVVMSample
using System;
using System.Windows.Input;
namespace WpfMVVMSample.Commands
{
public class DelegateCommand : ICommand
{
private Action _execute;
private Func<bool> _canExecute;
public DelegateCommand(Action execute, Func<bool> canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
public void Execute(object parameter)
{
_execute();
}
public bool CanExecute(object parameter)
{
return _canExecute();
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
}
}
<Window x:Class="WpfMVVMSample.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:WpfMVVMSample"
mc:Ignorable="d"
xmlns:vm="clr-namespace:WpfMVVMSample.ViewModels"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<vm:MainViewModel />
</Window.DataContext>
<Window.InputBindings>
<KeyBinding Gesture="Enter"
Command="{Binding Path=ShowSelectedItemCommand}" />
</Window.InputBindings>
<Grid>
<ListBox ItemsSource="{Binding Path=Items}"
SelectedItem="{Binding Path=SelectedItem}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0},{1}">
<Binding Path="Name" />
<Binding Path="Age" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
using System.Windows;
namespace WpfMVVMSample
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
namespace WpfMVVMSample.Models
{
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Person(string name, int age)
{
this.Name = name;
this.Age = age;
}
}
}
using System.Windows;
using System.ComponentModel; // INotifyPropertyChanged
using System.Collections.ObjectModel; // ObservableCollection
using WpfMVVMSample.Commands;
using WpfMVVMSample.Models;
namespace WpfMVVMSample.ViewModels
{
public class MainViewModel : INotifyPropertyChanged
{
// ---------------------------------------------------------------------
// プロパティ - リスト
private ObservableCollection<Person> _items;
public ObservableCollection<Person> Items
{
get { return _items; }
set
{
_items = value;
this.OnPropertyChanged(nameof(Items));
}
}
public Person SelectedItem { get; set; }
// ---------------------------------------------------------------------
// プロパティ - コマンド
private DelegateCommand _showSelectedItemCommand;
public DelegateCommand ShowSelectedItemCommand
{
get
{
if (_showSelectedItemCommand == null)
{
_showSelectedItemCommand = new DelegateCommand(
() => this.DoShowSelectedItem(),
() => this.SelectedItem != null
);
}
return _showSelectedItemCommand;
}
}
private void DoShowSelectedItem()
{
string message = string.Format("{0},{1}",
this.SelectedItem.Name, this.SelectedItem.Age);
MessageBox.Show(message);
}
// ---------------------------------------------------------------------
// コンストラクタ
public MainViewModel()
{
// 動作テスト用データ
this.Items = new ObservableCollection<Person>();
this.Items.Add(new Person("太郎", 39));
this.Items.Add(new Person("花子", 34));
this.Items.Add(new Person("次郎", 28));
this.Items.Add(new Person("良子", 22));
this.Items.Add(new Person("三郎", 15));
}
// ---------------------------------------------------------------------
// インターフェースの実装 - INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment