1、提示框 分為提示、異常、失敗、成功幾種類型 方法: /// <summary> /// 彈出提示 /// 標題:提示 /// </summary> /// <param name="strContent">內容</param> public static void ShowInfoMessage ...
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,再應用也不錯。