如何在棱镜中自定义弹出设置宽度和高度?

时间:2018-02-08 12:00:54

标签: .net wpf prism

问题陈述是:

我在我的wpf项目中使用自定义弹出窗口。我可以使用自定义弹出窗口和自定义用户控件。在那里,我可以在用户控件属性中提供宽度和高度,并且弹出窗口大小也是定义的。但问题是当我调整弹出窗口的大小时,额外的地方充满了空白空间,但我已将自定义控件设置为网格并设置为相应调整大小。但是如果我没有在自定义控件中设置宽度和高度,则调整大小的效果非常好。我的问题是:有什么方法可以告诉棱镜设置窗口大小,如在用户控件中定义的那样。

非常感谢任何帮助。

这用于shell属性:

    <Window x:Class="TestApp.Shell"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:prism="http://prismlibrary.com/"
xmlns:inf="clr-namespace:Application.Infrastructure;assembly=Application.Infrastructure"
        xmlns:infBehaviors="clr-Application.Infrastructure.Behaviors;assembly=Application.Infrastructure"
            infBehaviors:RegionPopupBehaviors.CreatePopupRegionWithName="{x:Static inf:RegionNames.SecondaryRegion}"
            infBehaviors:RegionPopupBehaviors.ContainerWindowStyle="{StaticResource WindowRegionStyle}"
            mc:Ignorable="d" Title="Title here"  WindowState="Maximized"
            WindowStyle="ThreeDBorderWindow" Background="#e5f8ff" Icon="Resources/favicon.ico">

以下是infBehaviors类

using Microsoft.Practices.ServiceLocation;
using Prism.Regions;
using System;
using System.ComponentModel;
using System.Windows;

namespace Application.Infrastructure.Behaviors
{
    /// <summary>
    /// Declares the Attached Properties and Behaviors for implementing Popup regions.
    /// </summary>
    /// <remarks>
    /// Although the fastest way is to create a RegionAdapter for a Window and register it with the RegionAdapterMappings,
    /// this would be conceptually incorrect because we want to create a new popup window everytime a view is added 
    /// (instead of having a Window as a host control and replacing its contents everytime Views are added, as other adapters do).
    /// This is why we have a different class for this behavior, instead of reusing the <see cref="RegionManager.RegionNameProperty"/> attached property.
    /// </remarks>
    public static class RegionPopupBehaviors
    {
        /// <summary>
        /// The name of the Popup <see cref="IRegion"/>.
        /// </summary>
        public static readonly DependencyProperty CreatePopupRegionWithNameProperty =
            DependencyProperty.RegisterAttached("CreatePopupRegionWithName", typeof(string), typeof(RegionPopupBehaviors), new PropertyMetadata(CreatePopupRegionWithNamePropertyChanged));

        /// <summary>
        /// The <see cref="Style"/> to set to the Popup.
        /// </summary>
        public static readonly DependencyProperty ContainerWindowStyleProperty =
          DependencyProperty.RegisterAttached("ContainerWindowStyle", typeof(Style), typeof(RegionPopupBehaviors), null);

        /// <summary>
        /// Gets the name of the Popup <see cref="IRegion"/>.
        /// </summary>
        /// <param name="owner">Owner of the Popup.</param>
        /// <returns>The name of the Popup <see cref="IRegion"/>.</returns>
        public static string GetCreatePopupRegionWithName(DependencyObject owner)
        {
            if (owner == null)
            {
                throw new ArgumentNullException("owner");
            }

            return owner.GetValue(CreatePopupRegionWithNameProperty) as string;
        }

        /// <summary>
        /// Sets the name of the Popup <see cref="IRegion"/>.
        /// </summary>
        /// <param name="owner">Owner of the Popup.</param>
        /// <param name="value">Name of the Popup <see cref="IRegion"/>.</param>
        public static void SetCreatePopupRegionWithName(DependencyObject owner, string value)
        {
            if (owner == null)
            {
                throw new ArgumentNullException("owner");
            }

            owner.SetValue(CreatePopupRegionWithNameProperty, value);
        }

        /// <summary>
        /// Gets the <see cref="Style"/> for the Popup.
        /// </summary>
        /// <param name="owner">Owner of the Popup.</param>
        /// <returns>The <see cref="Style"/> for the Popup.</returns>
        public static Style GetContainerWindowStyle(DependencyObject owner)
        {
            if (owner == null)
            {
                throw new ArgumentNullException("owner");
            }

            return owner.GetValue(ContainerWindowStyleProperty) as Style;
        }

        /// <summary>
        /// Sets the <see cref="Style"/> for the Popup.
        /// </summary>
        /// <param name="owner">Owner of the Popup.</param>
        /// <param name="style"><see cref="Style"/> for the Popup.</param>
        public static void SetContainerWindowStyle(DependencyObject owner, Style style)
        {
            if (owner == null)
            {
                throw new ArgumentNullException("owner");
            }

            owner.SetValue(ContainerWindowStyleProperty, style);
        }

        /// <summary>
        /// Creates a new <see cref="IRegion"/> and registers it in the default <see cref="IRegionManager"/>
        /// attaching to it a <see cref="DialogActivationBehavior"/> behavior.
        /// </summary>
        /// <param name="owner">The owner of the Popup.</param>
        /// <param name="regionName">The name of the <see cref="IRegion"/>.</param>
        /// <remarks>
        /// This method would typically not be called directly, instead the behavior 
        /// should be set through the Attached Property <see cref="CreatePopupRegionWithNameProperty"/>.
        /// </remarks>
        public static void RegisterNewPopupRegion(DependencyObject owner, string regionName)
        {
            // Creates a new region and registers it in the default region manager.
            // Another option if you need the complete infrastructure with the default region behaviors
            // is to extend DelayedRegionCreationBehavior overriding the CreateRegion method and create an 
            // instance of it that will be in charge of registering the Region once a RegionManager is
            // set as an attached property in the Visual Tree.
            IRegionManager regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
            if (regionManager != null)
            {
                IRegion region = new SingleActiveRegion();
                DialogActivationBehavior behavior;
                behavior = new WindowDialogActivationBehavior();
                behavior.HostControl = owner;

                region.Behaviors.Add(DialogActivationBehavior.BehaviorKey, behavior);
                regionManager.Regions.Add(regionName, region);
            }
        }

        private static void CreatePopupRegionWithNamePropertyChanged(DependencyObject hostControl, DependencyPropertyChangedEventArgs e)
        {
            if (IsInDesignMode(hostControl))
            {
                return;
            }

            RegisterNewPopupRegion(hostControl, e.NewValue as string);
        }

        private static bool IsInDesignMode(DependencyObject element)
        {
            // Due to a known issue in Cider, GetIsInDesignMode attached property value is not enough to know if it's in design mode.
            return DesignerProperties.GetIsInDesignMode(element) || Application.Current == null
                   || Application.Current.GetType() == typeof(Application);
        }
    }
}

我用以下代码调用弹出窗口:

_regionManager.Regions[RegionNames.MainRegion].RequestNavigate("/AppSettingsView");

1 个答案:

答案 0 :(得分:1)

我很欣赏这个问题已经很老了,但希望这能帮助任何尝试做同样事情的人!

在棱镜库中,您可以访问一项名为对话服务的功能,该功能允许您轻松调用弹出窗口并完全访问其窗口属性。

为了创建注册对话框,创建一个模块并使用容器注册表将其注册为对话框。您使用的视图模型必须继承 IDialogAware 才能工作。

 containerRegistry.RegisterDialog<View, ViewModel>("DialogName");

然后从任何地方打开对话框

dialogService.ShowDialog("DialogName")

使用此模式时,您可以使用用户控件中的prism:Dialog.WindowStyle 标记设置窗口的属性

<UserControl x:Class="TestApp.Shell"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
       xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
       xmlns:prism="http://prismlibrary.com">
   <prism:Dialog.WindowStyle>
       <Style TargetType="Window">
           <Setter Property="ResizeMode" Value="NoResize" />
           <Setter Property="Width" Value="880" />
           <Setter Property="Height" Value="780" />
       </Style>
   </prism:Dialog.WindowStyle>
</UserControl>

您可以在对话调用中使用额外的参数,以便将数据传入和传出对话,对话服务的文档中对此进行了介绍 here