WPF 提示框、确认框、确认输入框

1、提示框

分为提示、异常、失败、成功几种类型

方法:

复制代码
        /// <summary>
        /// 弹出提示
        /// 标题:提示
        /// </summary>
        /// <param name="strContent">内容</param>
        public static void ShowInfoMessageBox(string strContent)
        {
            AlertRadWindow(Application.Current.MainWindow, strContent, Application.Current.Resources["MessageInfoStyle"] as Style, "提示");
        }
        /// <summary>
        /// 弹出提示
        /// 标题:异常
        /// </summary>
        /// <param name="strContent">内容</param>
        public static void ShowExceptionMessageBox(string strContent)
        {
            AlertRadWindow(Application.Current.MainWindow, strContent, Application.Current.Resources["MessageInfoStyle"] as Style, "异常");
        }
        /// <summary>
        /// 弹出提示
        /// 标题:失败
        /// </summary>
        /// <param name="strContent">内容</param>
        public static void ShowErrorMessageBox(string strContent)
        {
            AlertRadWindow(Application.Current.MainWindow, strContent, Application.Current.Resources["MessageErrorStyle"] as Style, "失败");
        }
        /// <summary>
        /// 弹出提示
        /// 标题:成功
        /// </summary>
        /// <param name="strContent">内容</param>
        public static void ShowSuccessMessageBox(string strContent)
        {
            AlertRadWindow(Application.Current.MainWindow, strContent, Application.Current.Resources["MessageSuccessStyle"] as Style, "成功");
        }
        
        private static void AlertRadWindow(ContentControl owner, string strContent, Style style, string header)
        {
            RadWindow.Alert(new DialogParameters()
            {
                Owner = owner,
                Content = new TextBlock() { Text = strContent, TextWrapping = TextWrapping.Wrap, VerticalAlignment = VerticalAlignment.Center },
                ContentStyle = style,
                Header = header,
            });
        }
View Code
复制代码

样式:

复制代码
    <Style x:Key="MessageInfoStyle" TargetType="{x:Type telerik:RadAlert}">
        <Setter Property="IsTabStop" Value="false"/>
        <Setter Property="MinWidth" Value="200"/>
        <Setter Property="Width" Value="auto"/>
        <Setter Property="MaxWidth" Value="400"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type telerik:RadAlert}">
                    <Grid x:Name="LayoutRoot">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" MinHeight="50"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <Image x:Name="Icon" Grid.Row="0" Grid.Column="0" Margin="8" VerticalAlignment="Center" Height="30" Source="..\..\Images\Caption\tishi .png"/>
                        <ContentPresenter x:Name="AlertText" Grid.Row="0" Grid.Column="1" Margin="0,8,8,8" VerticalAlignment="Center"/>
                        <Border x:Name="HorizontalRule" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" BorderBrush="Gainsboro" Height="2" BorderThickness="0 0 0 1"/>
                        <telerik:RadButton x:Name="OK" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Right" Margin="8,5,8,5" MinWidth="70" Content="确定"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="MessageErrorStyle" TargetType="{x:Type telerik:RadAlert}">
        <Setter Property="IsTabStop" Value="false"/>
        <Setter Property="MinWidth" Value="200"/>
        <Setter Property="Width" Value="auto"/>
        <Setter Property="MaxWidth" Value="400"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type telerik:RadAlert}">
                    <Grid x:Name="LayoutRoot">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" MinHeight="50"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <Image x:Name="Icon" Grid.Row="0" Grid.Column="0" Margin="8" VerticalAlignment="Center" Height="30" Source="..\..\Images\Caption\error.png"/>
                        <ContentPresenter x:Name="AlertText" Grid.Row="0" Grid.Column="1" Margin="0,8,8,8" VerticalAlignment="Center"/>
                        <Border x:Name="HorizontalRule" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" BorderBrush="Gainsboro" Height="2" BorderThickness="0 0 0 1"/>
                        <telerik:RadButton x:Name="OK" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Right" Margin="8,5,8,5" MinWidth="70" Content="确定"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="MessageSuccessStyle" TargetType="{x:Type telerik:RadAlert}">
        <Setter Property="IsTabStop" Value="false"/>
        <Setter Property="MinWidth" Value="200"/>
        <Setter Property="Width" Value="auto"/>
        <Setter Property="MaxWidth" Value="400"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type telerik:RadAlert}">
                    <Grid x:Name="LayoutRoot">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" MinHeight="50"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <Image x:Name="Icon" Grid.Row="0" Grid.Column="0" Margin="8" VerticalAlignment="Center" Height="30" Source="..\..\Images\Caption\success.png"/>
                        <ContentPresenter x:Name="AlertText" Grid.Row="0" Grid.Column="1" Margin="0,8,8,8" VerticalAlignment="Center"/>
                        <Border x:Name="HorizontalRule" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" BorderBrush="Gainsboro" Height="2" BorderThickness="0 0 0 1"/>
                        <telerik:RadButton x:Name="OK" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Right" Margin="8,5,8,5" MinWidth="70" Content="确定"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
View Code
复制代码

2、确认框

后台方法:

复制代码
/// <summary>
        /// 确认对话框
        /// </summary>
        /// <param name="strContent">内容</param>
        /// <param name="onClosed">关闭事件 案例:new EventHandler WindowClosedEventArgs(OnClosed))</param>
        /// <param name="okButtonContent">确定按钮内容</param>
        /// <param name="cancelButtonContent">取消按钮内容</param>
        public static void ShowConfirmMessageBox(string strContent, EventHandler<WindowClosedEventArgs> onClosed, string okButtonContent, string cancelButtonContent)
        {
            Style style = Application.Current.Resources["MessageConfirmStyle"] as Style;
            RadWindow.Confirm(new DialogParameters()
            {
                Owner = Application.Current.MainWindow,
                Header = "确认",
                ContentStyle = style,
                Content = new TextBlock() { Text = strContent, TextWrapping = TextWrapping.Wrap, VerticalAlignment = VerticalAlignment.Center },
                Closed = onClosed,
                OkButtonContent = okButtonContent,
                CancelButtonContent = cancelButtonContent,
            });
        }
View Code
复制代码

样式设置:

复制代码
    <Style x:Key="MessageConfirmStyle" TargetType="{x:Type telerik:RadConfirm}">
        <Setter Property="IsTabStop" Value="false"/>
        <Setter Property="MinWidth" Value="200"/>
        <Setter Property="Width" Value="auto"/>
        <Setter Property="MaxWidth" Value="400"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type telerik:RadConfirm}">
                    <Grid x:Name="LayoutRoot">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" MinHeight="50"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Image x:Name="Icon" Grid.Row="0" Grid.Column="0" Margin="8" VerticalAlignment="Center" Height="30" Source="..\..\Images\Caption\help.png"/>
                        <ContentPresenter x:Name="AlertText" Grid.Row="0" Grid.Column="1" Margin="0,8,8,8" VerticalAlignment="Center"/>

                        <Border x:Name="HorizontalRule" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" BorderBrush="Gainsboro" Height="2" BorderThickness="0 0 0 1"/>

                        <Grid Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition></ColumnDefinition>
                                <ColumnDefinition Width="90"></ColumnDefinition>
                            </Grid.ColumnDefinitions>
                            <telerik:RadButton x:Name="OK" HorizontalAlignment="Right" Margin="8,5,8,5" MinWidth="70" Grid.Column="0" />
                            <telerik:RadButton x:Name="Cancel" HorizontalAlignment="Right" Margin="8,5,8,5" MinWidth="70" Grid.Column="1" />
                        </Grid>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
View Code
复制代码

3、确认输入对话框

方法:

复制代码
        /// <summary>
        /// 输入确认对话框
        /// </summary>
        /// <param name="strContent">内容</param>
        /// <param name="onClosed">关闭事件 案例:new EventHandler WindowClosedEventArgs(OnClosed))</param>
        /// <param name="okButtonContent">确定按钮内容</param>
        /// <param name="cancelButtonContent">取消按钮内容</param>
        public static void ShowPromptMessageBox(string strContent, EventHandler<WindowClosedEventArgs> onClosed, string okButtonContent, string cancelButtonContent)
        {
            DialogParameters dialogParameters = new DialogParameters();
            dialogParameters.Owner = Application.Current.MainWindow;
            dialogParameters.Header = "确认";
            dialogParameters.Content = strContent;
            dialogParameters.OkButtonContent = okButtonContent;
            dialogParameters.CancelButtonContent = cancelButtonContent;
            dialogParameters.Closed = onClosed;
            RadWindow.Prompt(dialogParameters);
        }
View Code
复制代码

样式没有,可以自己自定义一个~

PS:可以将样式放入App.xaml中,然后后台通过 Application.Current.Resources就可以获取了。当然也可以将这些样式封装成UserCotrol,再应用也不错。

posted @   唐宋元明清2188  阅读(12525)  评论(1编辑  收藏  举报
编辑推荐:
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
点击右上角即可分享
微信分享提示
哥伦布
01:11发布
哥伦布
01:11发布
8°
西北风
2级
空气质量
相对湿度
81%
今天
中雨
3°/15°
周四
大雨
2°/14°
周五
小雨
4°/10°