LoginSignup
8
7

More than 5 years have passed since last update.

Prism 6.1のダイアログにMahAppsのMetroWindowを使う

Posted at

WPFのプログラムをMVVMパターンで記述する際に、ダイアログを用いたユーザーとの対話をどう記述するかについてさまざまなアプローチがある。そのうちの一つに、PrismのInteraction Requestを用いる方法がある。

この方法でダイアログを表示するときの問題は、MahAppsのようなサードパーティのウィンドウを使っているときに、普通のウィンドウが使われてしまうため外観を統一できなくなることだ。

これを避けるには、まずPrismのPopupWindowActionを継承してサブクラスを作り、CreateWindow()をオーバーライドしてMahAppsのMetroWindowを生成する。ただし、CreateWindow()はPrism 6.0以降にしかないので注意してほしい。

using System.Windows;
using MahApps.Metro.Controls;
using Prism.Interactivity;

namespace Sample
{
    internal class MetroPopupWindowAction : PopupWindowAction
    {
        protected override Window CreateWindow()
        {
            return new MetroWindow
            {
                Style = new Style(),
                SizeToContent = SizeToContent.WidthAndHeight
            };
        }
    }
}

どのダイアログにも共通するプロパティはここで指定しておく。Styleには空のスタイルを設定しておかないと、なぜかダイアログを表示するタイミングでArgumentNullExceptionが出てしまう。

このMetroPopupWindowActionを、InteractionRequestTriggerのアクションとして指定する。表示するダイアログのスタイルはWindowStyleで指定する。ただし、TargetTypeWindow以外のクラスを指定できるのは、Prism 6.1からである。

<prism:InteractionRequestTrigger SourceObject="{Binding ConfirmationRequest, Mode=OneWay}">
    <local:MetroPopupWindowAction IsModal="True" CenterOverAssociatedObject="True">
        <local:MetroPopupWindowAction.WindowContent>
            <local:ConfirmDialog/>
        </local:MetroPopupWindowAction.WindowContent>
        <local:MetroPopupWindowAction.WindowStyle>
            <Style TargetType="controls:MetroWindow">
                <Setter Property="ResizeMode" Value="NoResize"/>
                <Setter Property="ShowInTaskbar" Value="False"/>
            </Style>
        </local:MetroPopupWindowAction.WindowStyle>
    </local:MetroPopupWindowAction>
</prism:InteractionRequestTrigger>

Prism 6.0より前でも、PopupWindowActionのソースコードを参考にGetWindow()をオーバーライドする形でサブクラスを定義すれば、同じことができるはずだ。

8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7