Created
May 18, 2016 11:30
-
-
Save AtsushiSuzuki/c7b575ba1323eb7fe015019ad3d36ef8 to your computer and use it in GitHub Desktop.
ViewModel => Viewのイベント通知
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="WpfApplication1.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:WpfApplication1" | |
mc:Ignorable="d" | |
Title="MainWindow" Height="350" Width="525"> | |
<Grid> | |
<Button Content="Hello!" Command="{Binding DoSomething}"/> | |
</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; | |
using System.Windows; | |
using System.Windows.Data; | |
using System.Windows.Input; | |
using Prism.Commands; | |
namespace WpfApplication1 | |
{ | |
/// <summary> | |
/// MainWindow.xaml の相互作用ロジック | |
/// </summary> | |
public partial class MainWindow : Window | |
{ | |
public ICommand CloseCommand | |
{ | |
get { return (ICommand)this.GetValue(MainWindow.CloseCommandProperty); } | |
set { this.SetValue(MainWindow.CloseCommandProperty, value); } | |
} | |
public static readonly DependencyProperty CloseCommandProperty = | |
DependencyProperty.Register(nameof(CloseCommand), typeof(ICommand), typeof(MainWindow), new PropertyMetadata(default(ICommand))); | |
public MainWindow() | |
{ | |
this.DataContext = new ViewModel(); | |
this.InitializeComponent(); | |
this.SetBinding(CloseCommandProperty, new Binding() | |
{ | |
Path = new PropertyPath("Close"), | |
Mode = BindingMode.OneWayToSource, | |
}); | |
this.CloseCommand = new DelegateCommand(() => | |
{ | |
this.Close(); | |
}); | |
} | |
} | |
} |
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; | |
using System.Windows; | |
using System.Windows.Input; | |
using Prism.Commands; | |
namespace WpfApplication1 | |
{ | |
public class ViewModel | |
{ | |
public ICommand DoSomething { get; } | |
public ICommand Close { get; set; } | |
public ViewModel() | |
{ | |
this.DoSomething = new DelegateCommand(() => | |
{ | |
MessageBox.Show("Hello, world!"); | |
this.Close?.Execute(null); | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment