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

Posted 唐宋元明清的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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,再应用也不错。

以上是关于WPF 提示框确认框确认输入框的主要内容,如果未能解决你的问题,请参考以下文章

html网页怎么显示对话框,确认框,提示框

11.8所学

JavaScript 中创建三种消息框:警告框确认框提示框。

js中prompt()的用法

wpf实现QQ表情弹出框效果

如何使用 Jquery 使用户从确认框中的输入框中取消焦点?