ポップアップウインドウにビヘイビアをセットする


やりたいこと

Prismのポップアップウインドウにビヘイビアをセットする。

実装

PopupWindowActionには、スタイルは指定できるがビヘイビアは指定できない。そこで、ビヘイビアをスタイルで指定できるようにすることで、ポップアップウインドウにビヘイビアをセットする。

そのためにビヘイビアをスタイルとして追加できるコレクションクラスを用意する。

ビヘイビアのコレクション
using System.Linq;
using System.Windows;
using System.Windows.Interactivity;

namespace Sample {
    public class StyleBehaviorCollection : FreezableCollection<Behavior> {
        public static readonly DependencyProperty StyleBehaviorsProperty 
            = DependencyProperty.RegisterAttached(
                "StyleBehaviors",
                typeof(StyleBehaviorCollection),
                typeof(StyleBehaviorCollection),
                new PropertyMetadata(StyleBehaviors_PropertyChanged));

        public static StyleBehaviorCollection GetStyleBehaviors(DependencyObject obj)
            => (StyleBehaviorCollection)obj.GetValue(StyleBehaviorsProperty);

        public static void SetStyleBehaviors(DependencyObject obj, StyleBehaviorCollection value)
            => obj.SetValue(StyleBehaviorsProperty, value);

        protected override Freezable CreateInstanceCore()
            => new StyleBehaviorCollection();

        private static void StyleBehaviors_PropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) {
            if (e.OldValue == e.NewValue)
                return;

            var value = e.NewValue as StyleBehaviorCollection;
            if (value == null)
                return;

            var behaviors = Interaction.GetBehaviors(sender);
            behaviors.Clear();
            foreach (var b in value.Select(x => (Behavior)x.Clone())) {
                behaviors.Add(b);
            }
        }
    }
}

使い方

XAML
<Window x:Class="Sample.MainView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:prism="http://prismlibrary.com/"
        xmlns:local="clr-namespace:Sample"
        Title="MainView" Height="300" Width="300">
    <i:Interaction.Triggers>
        <prism:InteractionRequestTrigger SourceObject="{Binding OpenXXXDialogRequest}">
            <prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True" >
                <prism:PopupWindowAction.WindowContent>
                    <local:XXXDialog />
                </prism:PopupWindowAction.WindowContent>
                <prism:PopupWindowAction.WindowStyle>
                    <Style TargetType="Window">
                        <Setter Property="local:StyleBehaviorCollection.StyleBehaviors">
                            <Setter.Value>
                                <local:StyleBehaviorCollection>
                                    <local:YYYBehavior/>
                                </local:StyleBehaviorCollection>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </prism:PopupWindowAction.WindowStyle>
            </prism:PopupWindowAction>
        </prism:InteractionRequestTrigger>
    </i:Interaction.Triggers>
    <Grid>
        <TextBox />
    </Grid>
</Window>