0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Posted at

#やりたいこと
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>
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?