INotifyPropertyChanged
が面倒.プロパティの変化時に自動的に呼び出してほしいICommand
が面倒.ボタンをおしたときに直接メソッドを呼び出させてほしい
Last active
October 29, 2020 15:22
-
-
Save castaneai/f174b5d3c4668390bdd3 to your computer and use it in GitHub Desktop.
[C#, WPF] MVVMで簡単なアプリを作成
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Window x:Class="MVVMTest.MainWindow" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:vm="clr-namespace:MVVMTest.ViewModel" | |
Title="MainWindow" Height="111" Width="205"> | |
<Window.DataContext> | |
<vm:MainWindowViewModel /> | |
</Window.DataContext> | |
<Grid> | |
<Button Command="{Binding PushButtonCommand}" Content="これを押すと下の数字が1増える" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="170"/> | |
<TextBox HorizontalAlignment="Left" Margin="10,34,0,0" TextWrapping="Wrap" Text="{Binding Count}" Width="120" Height="23" VerticalAlignment="Top"/> | |
</Grid> | |
</Window> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Windows; | |
namespace MVVMTest | |
{ | |
public partial class MainWindow : Window | |
{ | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.ComponentModel; | |
using System.Windows.Input; | |
namespace MVVMTest.ViewModel | |
{ | |
public class MainWindowViewModel : INotifyPropertyChanged | |
{ | |
public int Count | |
{ | |
get | |
{ | |
return _count; | |
} | |
set | |
{ | |
_count = value; | |
if (PropertyChanged != null) { | |
PropertyChanged(this, new PropertyChangedEventArgs("Count")); | |
} | |
} | |
} | |
public ICommand PushButtonCommand | |
{ | |
get | |
{ | |
if (_pushButtonCommand == null) { | |
_pushButtonCommand = new PushButtonCommand(this); | |
} | |
return _pushButtonCommand; | |
} | |
} | |
private int _count; | |
private ICommand _pushButtonCommand; | |
public event PropertyChangedEventHandler PropertyChanged; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment