ViewModelを起点にViewを操作する方法としては、添付ビヘイビアやメッセンジャーなどの方法があります。
また、Prismを使っているのであればInteractionRequestの機能が用意されているので、これを使うのも良いかと思います。
InteractionRequestを利用するサンプルを載せておきます。
System.Windows.Interactivityを参照に追加してください。
MainWindow.xaml
xml
1<Window x:Class="TeraTail61250.MainWindow"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
7 xmlns:p="http://www.codeplex.com/prism"
8 xmlns:local="clr-namespace:TeraTail61250"
9 mc:Ignorable="d"
10 Title="MainWindow" Height="350" Width="525">
11 <!--↑ local はViewModelやActionクラスの名前空間に置き換えてください-->
12
13 <Window.DataContext>
14 <local:MainWindowViewModel />
15 </Window.DataContext>
16
17 <i:Interaction.Triggers>
18 <p:InteractionRequestTrigger SourceObject="{Binding ShowAnotherWindowRequest}">
19 <local:ShowAnotherWindowAction />
20 </p:InteractionRequestTrigger>
21 </i:Interaction.Triggers>
22
23 <Grid>
24 <Button Width="75" Height="30" Command="{Binding ShowAnotherWindowCommand}"/>
25 </Grid>
26</Window>
MainWindowViewModel.cs
C#
1public class MainWindowViewModel : BindableBase
2{
3 //トリガになるボタンのコマンド
4 public ICommand ShowAnotherWindowCommand { get; private set; }
5
6 //ViewのInteractionRequestTriggerからバインドするプロパティ
7 public InteractionRequest<Notification> ShowAnotherWindowRequest { get; private set; }
8
9 public MainWindowViewModel()
10 {
11 this.ShowAnotherWindowCommand = new DelegateCommand(showAnotherWindow);
12 this.ShowAnotherWindowRequest = new InteractionRequest<Notification>();
13 }
14
15 //コマンドの実行内容
16 private void showAnotherWindow()
17 {
18 //別ウィンドウのDataContextに使うオブジェクト
19 var vm = new AnotherWindowViewModel();
20
21 //requestのRaiseを呼び出す
22 ShowAnotherWindowRequest.Raise(new Notification { Content = vm });
23 }
24}
ShowAnotherWindowAction.cs
C#
1public class ShowAnotherWindowAction : TriggerAction<DependencyObject>
2{
3 protected override void Invoke(object parameter)
4 {
5 var args = parameter as InteractionRequestedEventArgs;
6 var vm = args.Context.Content;
7 var callerWindow = this.AssociatedObject as Window;
8
9 //親ウィンドウ非表示
10 callerWindow.Hide();
11
12 //別ウィンドウを生成、表示
13 new AnotherWindow() { DataContext = vm }.ShowDialog();
14
15 //親ウィンドウ再表示
16 callerWindow.Show();
17
18 //呼び出しが終わったことを伝えるコールバック
19 args.Callback.Invoke();
20 }
21}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/01/09 12:09