Skip to content

Instantly share code, notes, and snippets.

@AtsushiSuzuki
Created May 18, 2016 11:30
Show Gist options
  • Save AtsushiSuzuki/c7b575ba1323eb7fe015019ad3d36ef8 to your computer and use it in GitHub Desktop.
Save AtsushiSuzuki/c7b575ba1323eb7fe015019ad3d36ef8 to your computer and use it in GitHub Desktop.
ViewModel => Viewのイベント通知
<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>
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();
});
}
}
}
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