Prism 弹窗右上角按钮设置以及宽高自适应

一.无放大缩小按钮 但是点击右上角可以关闭
搭建好Prism框架后开始对DialogWindow和Dialog的UserControl设置
  1. window设置
 	<WindowChrome GlassFrameThickness="-1"/>

当 GlassFrameThickness=“1” 时候点击右上角无响应

完整代码

<Window x:Class="XXDialogWindow.DialogWindowEx"
        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:local="clr-namespace:XXDialogWindow"
        mc:Ignorable="d"
        Title="DialogWindowEx" Height="450" Width="800">
    <WindowChrome.WindowChrome>
        <WindowChrome GlassFrameThickness="-1"/>
    </WindowChrome.WindowChrome>
    <Grid>
        
    </Grid>
</Window>

二. 显示右上角窗口按钮

将DialogWindow 的窗体背景颜色Background设置为Transparent, 显示右上角的窗口按钮;将ResizeMode设置为NoMode,窗口按钮只保留退出x。

  1. Background=“Transparent”
<Window x:Class="XXDialogWindow.DialogWindowEx"
        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:local="clr-namespace:XXDialogWindow"
        mc:Ignorable="d" d:DesignHeight="200" d:DesignWidth="200" Height="480" Width="500" 
        Title="DialogWindowEx" Background="Transparent" >
    <WindowChrome.WindowChrome>
        <WindowChrome GlassFrameThickness="-1"/>
    </WindowChrome.WindowChrome>
    <Grid>
        
    </Grid>
</Window>

```![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/0130730a1caf4a048c4117aadc052b4f.png#pic_center)

 2.  ResizeMode="NoResize"
 

```csharp
<Window x:Class="XXDialogWindow.DialogWindowEx"
        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:local="clr-namespace:XXDialogWindow"
        mc:Ignorable="d" d:DesignHeight="200" d:DesignWidth="200" Height="480" Width="500" 
        Title="DialogWindowEx" Background="Transparent" ResizeMode="NoResize">
    <WindowChrome.WindowChrome>
        <WindowChrome GlassFrameThickness="-1"/>
    </WindowChrome.WindowChrome>
    <Grid>
        
    </Grid>
</Window>

在这里插入图片描述

二. 弹窗宽高自适应

目前已知有三种方式可以实现。

  1. 一种是DialogWindow的宽高做绑定
  Width="{Binding Content.Width,RelativeSource={RelativeSource Self}}"
  Height="{Binding Content.Height,RelativeSource={RelativeSource Self}}">
  1. DialogView采用prism style
<UserControl x:Class="XXDialogWindow.Views.TextView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:XXDialogWindow.Views"
             mc:Ignorable="d" 
             xmlns:p="http://prismlibrary.com/"
             d:DesignHeight="400" d:DesignWidth="800" >
    <p:Dialog.WindowStyle>
        <Style TargetType="Window">
            <Setter Property="Height" Value="300"/>
            <Setter Property="Width" Value="300"/>
            <Setter Property="p:Dialog.WindowStartupLocation" Value="CenterScreen" />
            <Setter Property="ResizeMode" Value="NoResize"/>
            <Setter Property="ShowInTaskbar" Value="False"/>
            <!--<Setter Property="SizeToContent" Value="WidthAndHeight"/>-->
            
        </Style>
    </p:Dialog.WindowStyle>
    <Grid>
        <TextBlock Text="Text" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="22"/>
    </Grid>
</UserControl>

Dialogwindow

<Window x:Class="XXDialogWindow.DialogWindowEx"
        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:local="clr-namespace:XXDialogWindow"
        mc:Ignorable="d"  Height="480" Width="500" 
        Title="DialogWindowEx" Background="Transparent" ResizeMode="NoResize">
    <WindowChrome.WindowChrome>
        <WindowChrome GlassFrameThickness="-1"/>
    </WindowChrome.WindowChrome>
    <Grid>
        
    </Grid>
</Window>

  1. 将Dialogwindow 的SizeToContent属性设置为WidthAndHeight,并设置DialogView的宽高
    Dialogwindow
<Window x:Class="XXDialogWindow.DialogWindowEx"
        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:local="clr-namespace:XXDialogWindow"
        mc:Ignorable="d"  Height="480" Width="500" SizeToContent="WidthAndHeight"
        Title="DialogWindowEx" Background="Transparent" ResizeMode="NoResize">
    <WindowChrome.WindowChrome>
        <WindowChrome GlassFrameThickness="-1"/>
    </WindowChrome.WindowChrome>
    <Grid>
        
    </Grid>
</Window>

DialogView

<UserControl x:Class="XXDialogWindow.Views.TextView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:XXDialogWindow.Views"
             mc:Ignorable="d" 
             xmlns:p="http://prismlibrary.com/"
             d:DesignHeight="400" d:DesignWidth="800" Height="200" Width="100">
    <p:Dialog.WindowStyle>
        <Style TargetType="Window">
            <!--<Setter Property="Height" Value="300"/>
            <Setter Property="Width" Value="300"/>-->
            <!--<Setter Property="p:Dialog.WindowStartupLocation" Value="CenterScreen" />
            <Setter Property="ResizeMode" Value="NoResize"/>
            <Setter Property="ShowInTaskbar" Value="False"/>-->
            <!--<Setter Property="SizeToContent" Value="WidthAndHeight"/>-->
            
        </Style>
    </p:Dialog.WindowStyle>
    <Grid>
        <TextBlock Text="Text" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="22"/>
    </Grid>
</UserControl>

WPF(Windows Presentation Foundation)是一种用于创建Windows应用程序的开发框架。而Prism是一种用于构建可扩展、模块化和可重用的WPF应用程序的开发框架。在WPF Prism中,弹窗可以通过对话框的方式来实现。 在WPF Prism中,可以使用对话框服务(DialogService)来创建和管理弹窗。DialogService提供了一系列用于显示、关闭和传递参数给弹窗的方法。可以通过注册DialogService服务来在整个应用程序中使用。 要创建一个弹窗,首先需要定义一个弹窗的View和ViewModel。View通常是一个UserControl,用于定义弹窗的界面。ViewModel负责处理弹窗的逻辑和与数据的交互。 在需要显示弹窗的地方,可以使用DialogService的Show方法来显示弹窗。Show方法接收一个字符串参数来指定要显示的弹窗的名称,该名称应与弹窗的View名称相对应。还可以通过Show方法传递要传递给弹窗的参数。 在ViewModel中,可以通过实现INavigationAware接口来获取传递给弹窗的参数。这样,在弹窗显示后,ViewModel就可以使用这些参数来进行必要的操作。 当需要关闭弹窗时,可以使用DialogService的Close方法来关闭弹窗。Close方法接收一个字符串参数来指定要关闭的弹窗的名称,该名称应与弹窗的View名称相对应。 总之,WPF Prism提供了弹窗的管理和控制的机制,通过DialogService可以创建、显示和关闭弹窗,使得应用程序具有更好的用户体验和交互性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值