首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在棱镜中设置自定义弹出的宽度和高度?

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

Stack Overflow用户
提问于 2018-02-08 12:00:54
回答 1查看 475关注 0票数 0

问题是:

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

任何帮助都是非常感谢的。

这在shell属性中使用:

代码语言:javascript
运行
复制
    <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类

代码语言:javascript
运行
复制
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);
        }
    }
}

我用下面的代码调用弹出窗口:

代码语言:javascript
运行
复制
_regionManager.Regions[RegionNames.MainRegion].RequestNavigate("/AppSettingsView");
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-16 08:48:33

我很感激这个问题已经很老了,但希望这能帮助任何想要做同样事情的人!

在棱镜库中,您可以访问一个名为对话框服务的特性,它允许您轻松地调用弹出窗口并对其窗口属性进行完全访问。

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

代码语言:javascript
运行
复制
 containerRegistry.RegisterDialog<View, ViewModel>("DialogName");

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

代码语言:javascript
运行
复制
dialogService.ShowDialog("DialogName")

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

代码语言:javascript
运行
复制
<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>

您可以在对话框调用中使用额外的参数,以便在对话框服务这里的文档中对数据进行来回传输。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48685003

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档