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

简介: 1、提示框 分为提示、异常、失败、成功几种类型 方法: /// /// 弹出提示 /// 标题:提示 /// /// 内容 public static void ShowInfo...

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,再应用也不错。

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。
目录
相关文章
|
C#
WPF 圆角输入框
原文:WPF 圆角输入框 今天打算来做一个圆角的输入框 默认输入框:   这个输入框不好看,并且在XP 跟 WIN 7  WIN10 效果 都不太一样   我们今天不用模板的方式,而是 最简单的方式 来实现 圆角 输入框;     ------------------------...
1376 0
|
C#
设置WPF输入框焦点
原文:设置WPF输入框焦点 在WPF中设置控件键盘焦点 Keyboard.Focus(/*控件名称*/);
1297 0
|
C# .NET 开发框架
WPF中设置TEXTBOX为多行文本输入框
WPF中没有textarea的东西,不像在ASP.NET中设置textbox那样设置一个多行属性就可以变成文本域,虽然可以使用ricktextbox实现多行文本输入,但是richtextbox比较复杂,面对简单的多行文本输入的时候太麻烦了点,但是WPF的textbox依然可以通过设置属性实现像textarea一样的多行文本输入。
1613 0
|
C#
WPF和Expression Blend开发实例:一个样式实现的数字输入框
原文:WPF和Expression Blend开发实例:一个样式实现的数字输入框 今天来一个比较奇淫技巧的手法,很少人用,同时也不推荐太过频繁的使用. 先上样式: ...
981 0
|
6月前
|
C# 开发者 Windows
基于Material Design风格开源、易用、强大的WPF UI控件库
基于Material Design风格开源、易用、强大的WPF UI控件库
389 0
|
6月前
|
C#
浅谈WPF之装饰器实现控件锚点
使用过visio的都知道,在绘制流程图时,当选择或鼠标移动到控件时,都会在控件的四周出现锚点,以便于修改大小,移动位置,或连接线等,那此功能是如何实现的呢?在WPF开发中,想要在控件四周实现锚点,可以通过装饰器来实现,今天通过一个简单的小例子,简述如何在WPF开发中,应用装饰器,仅供学习分享使用,如有不足之处,还请指正。
142 1
|
3月前
|
开发框架 缓存 前端开发
循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(11) -- 下拉列表的数据绑定以及自定义系统字典列表控件
循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(11) -- 下拉列表的数据绑定以及自定义系统字典列表控件
|
3月前
|
C# 开发者 Windows
一款基于Fluent设计风格、现代化的WPF UI控件库
一款基于Fluent设计风格、现代化的WPF UI控件库
|
3月前
|
C# Windows
WPF中如何使用HandyCotrol控件库
WPF中如何使用HandyCotrol控件库
195 1
|
3月前
|
C# 前端开发 UED
WPF数据验证实战:内置控件与自定义规则,带你玩转前端数据验证,让你的应用程序更上一层楼!
【8月更文挑战第31天】在WPF应用开发中,数据验证是确保输入正确性的关键环节。前端验证能及时发现错误,提升用户体验和程序可靠性。本文对比了几种常用的WPF数据验证方法,并通过示例展示了如何使用内置验证控件(如`TextBox`)及自定义验证规则实现有效验证。内置控件结合`Validation`类可快速实现简单验证;自定义规则则提供了更灵活的复杂逻辑支持。希望本文能帮助开发者更好地进行WPF数据验证。
116 0