WPF(MVVM 패턴)개발을 위해 Caliburn.Micro를 사용할 때 Event 처리하는 방법을 살펴보자. xaml파일에 ‘Message.Attach’, ‘Command’, 기본 바인딩을 작성하고 ViewModel 클래스 파일에서 해당 이벤트를 처리한다. 기본 골격을 위한 세부 내용은 ‘Caliburn.Micro, MVVM 기반 WPF’, ‘ICommand, RelayCommand in WPF’를 참고한다.
ShellView.xaml
<Window x:Class="WpfApp1.ShellView"
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:cal="http://www.caliburnproject.org"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
mc:Ignorable="d"
Title="CALIBURN MICRO" Height="450" Width="800">
<Grid>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Button Content="Click me" Width="100" Height="40" x:Name="TestButton" />
<Button Content="Click me 0" Width="100" Height="40"
cal:Message.Attach="[Event Click] = [Action TestButton0($source)]" />
<Button Content="Click me 1" Width="100" Height="40"
cal:Message.Attach="[Event PreviewMouseUp] = [Action HelloClicked1($eventArgs)]" />
<Button Content="Click me 2" Width="100" Height="40" x:Name="TestButton2"
cal:Message.Attach="[Event PreviewMouseUp] = [Action HelloClicked2($source, $eventArgs)]" />
<Button Content="Click me 3" Width="100" Height="40" x:Name="TestButton3"
Command="{Binding ButtonTest}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}}" />
<Button Content="Click me 4" Width="100" Height="40">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding ButtonClick}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type Button}}}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</StackPanel>
</Grid>
</Window>
ShellViewModel.cs
using Caliburn.Micro;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WpfApp1.ViewModels;
namespace WpfApp1
{
public class ShellViewModel : Conductor<IScreen>.Collection.OneActive
{
public void TestButton()
{
MessageBox.Show("TestButton Clicked!");
}
public void TestButton0(object sender)
{
if (!(sender is Button btn)) return;
MessageBox.Show($"TestButton0 : {btn.Content}");
}
public void HelloClicked1(MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
MessageBox.Show($"HelloClicked1 : {e.ButtonState.ToString()}");
e.Handled = true;
}
public void HelloClicked2(object sender, MouseButtonEventArgs e)
{
//var btn = sender as Button;
if (e.ChangedButton == MouseButton.Left && sender is Button btn)
MessageBox.Show($"HelloClicked2 : {btn.Name}, {e.ButtonState.ToString()}");
e.Handled = true;
}
public ICommand ButtonTest => new RelayCommand<object>(ButtonTestRun, o => o is Button);
private void ButtonTestRun(object obj)
{
if (!(obj is Button btn)) return;
MessageBox.Show($"ButtonTestClick : {btn.Name}, {btn.Content}");
}
public ICommand ButtonClick => new RelayCommand<object>(ButtonClickRun, o => o is Button);
private void ButtonClickRun(object obj)
{
if (!(obj is Button btn)) return;
MessageBox.Show($"ButtonClick : {btn.Content}");
}
}
}