UserControl中的prism PopupWindowAction

时间:2017-04-28 08:44:36

标签: c# wpf prism

如何在棱镜PopupWindowAction上隐藏Min,Max,Close按钮?

<UserControl .........

<prism:InteractionRequestTrigger SourceObject="{Binding NotificationRequest, Mode=OneWay}">            
       <prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True"/>
</prism:InteractionRequestTrigger>

我试过以下

<prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True">
            <prism:PopupWindowAction.WindowStyle>
                <Style TargetType="Window">
                    <Setter Property="ResizeMode" Value="NoResize"/>
                    <Setter Property="SizeToContent" Value="WidthAndHeight"/>
                </Style>
            </prism:PopupWindowAction.WindowStyle>
</prism:PopupWindowAction>

但它有构建错误: &#34;会员&#34; WindowStyle&#34;无法识别或无法访问&#34;

1 个答案:

答案 0 :(得分:1)

您可以从Prism PopupWindowAction继承并编写自己的实现。

我曾经写过一个具有以下功能的CustomPopupWindowAction:

  • CustomPopupWindowAction是无边框的。没有MinMaxHide按钮
  • 您可以通过鼠标左键拖动它
  • 拖动仅限于shell的边框
  • 移动shell也会移动CustomPopupWindowAction
  • 您可以拥有多个CustomPopupWindowAction
  • 计算shell的MinWidth和MinHeight,因此shell永远不会失去&#34;它的CustomPopupWindowActions

这里给你一个想法是我的例子:

如何在XAML中使用

<interact:Interaction.Triggers>

    <prism:InteractionRequestTrigger x:Name="_PopUpDialog" SourceObject="{Binding Path=PopUpDialog, Mode=OneWay,  UpdateSourceTrigger=PropertyChanged}">

        <local:CustomPopUpWindowAction 
                                        WindowContent="{Binding Path=TargetView, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
                                        CenterOverAssociatedObject="True"
                                        IsModal="False"
        >

        </local:CustomPopUpWindowAction >

    </prism:InteractionRequestTrigger>

</interact:Interaction.Triggers>

<强> C#

public class CustomPopUpWindowAction: PopupWindowAction
{
    private Window _Shell = null;
    private List<Window> _Windows = new List<Window>();
    private double _Top = 0;
    private double _Left = 0;


    public CustomPopUpWindowAction()
    {
        _Shell = Application.Current.Windows[0];
        _Top = _Shell.Top;
        _Left = _Shell.Left;
        _Shell.LocationChanged += _Shell_LocationChanged;
        _Shell.SizeChanged += _Shell_SizeChanged;
    }

    ~CustomPopUpWindowAction() 
    {
        _Shell.LocationChanged -= _Shell_LocationChanged;
        _Shell.SizeChanged -= _Shell_SizeChanged;

        foreach (Window w in _Windows)
        {
            w.MouseLeftButtonDown += OnChildWindowLeftMouseButtonDown;
            w.Closed += OnChildWindowClosed;
            w.LocationChanged += OnChildWindowPositionChanged;
        }
    }

    private void _Shell_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        Window shell = sender as Window;

        if (shell != null)
        {
            Double minWidth = 0;
            Double minHeight = 0;

            Double maxLeft = 0;
            Double maxLeftWith = 0;
            Double maxTop = 0;
            Double maxTopHeight = 0;

            Double border = 10;

            foreach (Window w in _Windows)
            {
                if (w.Left > maxLeft)
                {
                    maxLeft = w.Left;
                    maxLeftWith = w.Width;
                }
                if (w.Top > maxTop)
                {
                    maxTop = w.Top;
                    maxTopHeight = w.Height;
                }
            }

            minWidth = maxLeft + maxLeftWith - shell.Left + border;
            minHeight = maxTop + maxTopHeight - shell.Top + border;

            if (shell.Width < minWidth) shell.Width = minWidth;
            if (shell.Height < minHeight) shell.Height = minHeight;
        }
    }

    private void _Shell_LocationChanged(object sender, EventArgs e)
    {
        Window window = sender as Window;
        if (window != null)
        {
            Double differenceTop = _Top - window.Top;
            Double differenceLeft = _Left - window.Left;

            foreach (Window w in _Windows)
            {
                w.Top = w.Top - differenceTop;
                w.Left = w.Left - differenceLeft;
            }

            _Top = window.Top;
            _Left = window.Left;
        }
    }


    protected override Window CreateWindow()
    {
        _Windows.Add(base.CreateWindow());

        int index = _Windows.Count -1;

        _Windows[index].Owner = _Shell;

        _Windows[index].WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;

        _Windows[index].AllowsTransparency = true;

        _Windows[index].WindowStyle = System.Windows.WindowStyle.None;

        _Windows[index].BorderThickness = new Thickness(0);

        _Windows[index].ResizeMode = ResizeMode.NoResize;

        _Windows[index].MouseLeftButtonDown += OnChildWindowLeftMouseButtonDown;

        _Windows[index].Closed += OnChildWindowClosed;

        _Windows[index].LocationChanged += OnChildWindowPositionChanged; 

        _Windows[index].Tag = index;

        return _Windows[index];
    }

    private void OnChildWindowPositionChanged(object sender, EventArgs e)
    {
        double topBorder = 60;
        double leftBorder = 10;
        double rightBorder = 10;
        double bottomBorder = 10;

        Window window = sender as Window;

        if (window != null)
        {
            if (window.Top < _Shell.Top + topBorder) window.Top = _Shell.Top + topBorder;
            if (window.Left < _Shell.Left + leftBorder) window.Left = _Shell.Left + leftBorder;

            double maxLeft = _Shell.Left + _Shell.ActualWidth - window.Width - rightBorder ;
            if (window.Left > maxLeft) window.Left = maxLeft;

            double maxTop = _Shell.Top + _Shell.ActualHeight - window.Height - bottomBorder;
            if (window.Top > maxTop) window.Top = maxTop;
        }
    }

    private void OnChildWindowLeftMouseButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        Window window = sender as Window;
        if (window != null)
        {
            window.DragMove();
        }
    }

    private void OnChildWindowClosed(object sender, EventArgs e)
    {
        Window window = sender as Window;
        if (window != null)
        {
            window.MouseLeftButtonDown -= OnChildWindowLeftMouseButtonDown;
            window.Closed -= OnChildWindowClosed;
            window.LocationChanged -= OnChildWindowPositionChanged;
            _Windows.Remove(window);
        }
    }
}

当然,仍然有很大的改进空间和一些重构以提高可读性,但它运作良好。

对于您自己的自定义实现,只需查看CreateWindow();

玩得开心玩得开心!

顺便说一句:当然,通过这种实现,您必须注意,您显示的控件继承自IInteractionRequestAware,以便您可以完成交互。