<Window x:Class="Sample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:local="clr-namespace:Sample"
Height="350" Width="525"
Name="dialog"
>
<Window.DataContext>
<local:ViewModel x:Name="viewModel" />
</Window.DataContext>
<Grid>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Bottom" >
<Button Content="From View" Margin="5" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:ChangePropertyAction TargetObject="{Binding ElementName=dialog}" PropertyName="DialogResult" Value="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
<Button Content="From ViewModel" Margin="5" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:CallMethodAction TargetObject="{Binding ElementName=viewModel}" MethodName="Execute" />
</i:EventTrigger>
<ei:DataTrigger Binding="{Binding Path=CloseFlag}" Value="True">
<ei:ChangePropertyAction TargetObject="{Binding ElementName=dialog}" PropertyName="DialogResult" Value="True" />
</ei:DataTrigger>
</i:Interaction.Triggers>
</Button>
</StackPanel>
</Grid>
</Window>
public class ViewModel : INotifyPropertyChanged
{
/// <summary>
/// プロパティ変更イベントハンドラ
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// プロパティ変更処理
/// </summary>
/// <param name="name">プロパティ名</param>
public void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
/// <summary>
/// 終了フラグ
/// </summary>
private bool closeFlag = false;
/// <summary>
/// 終了フラグを取得または設定します。
/// </summary>
public bool CloseFlag
{
get { return closeFlag; }
set { closeFlag = value; OnPropertyChanged("CloseFlag"); }
}
/// <summary>
/// 実行
/// </summary>
public void Execute()
{
// execute process
// Close
CloseFlag = true;
}
}
0 件のコメント:
コメントを投稿