封装了Popup

代码如下:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Threading;

namespace Yuzifu.Tools
{
    public sealed class MessagePopup
    {
        public static void Show(UIElement parent, string message)
        {
            parent.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() =>
                {
                    Popup popup = new Popup();
                    popup.StaysOpen = true;
                    popup.PlacementTarget = parent;
                    popup.Placement = PlacementMode.Center;

                    Border aroundBorder = new Border();
                    aroundBorder.BorderThickness = new Thickness(2);
                    aroundBorder.BorderBrush = Brushes.SteelBlue;

                    StackPanel aroundStackPanel = new StackPanel();
                    aroundStackPanel.MinWidth = 320;
                    aroundStackPanel.MinHeight = 120;
                    aroundStackPanel.MaxWidth = 480;
                    aroundStackPanel.Background = Brushes.White;
                    aroundStackPanel.Orientation = Orientation.Vertical;
                    aroundStackPanel.FlowDirection = FlowDirection.LeftToRight;

                    DockPanel headDockPanel = new DockPanel();
                    headDockPanel.VerticalAlignment = VerticalAlignment.Top;
                    headDockPanel.Background = Brushes.SteelBlue;

                    TextBlock headTextBlock = new TextBlock();
                    headTextBlock.Margin = new Thickness(10, 0, 0, 0);
                    headTextBlock.Text = "提示";
                    headTextBlock.Height = 26;
                    headTextBlock.HorizontalAlignment = HorizontalAlignment.Left;
                    headTextBlock.VerticalAlignment = VerticalAlignment.Center;
                    headTextBlock.FontSize = 16;
                    headTextBlock.Focusable = false;
                    headTextBlock.IsHitTestVisible = false;
                    headTextBlock.Background = Brushes.SteelBlue;
                    headTextBlock.Foreground = Brushes.White;

                    TextBlock messageTextBlock = new TextBlock();
                    messageTextBlock.Text = message;
                    messageTextBlock.Margin = new Thickness(20, 20, 20, 10);
                    messageTextBlock.MinHeight = 28;
                    messageTextBlock.VerticalAlignment = VerticalAlignment.Top;
                    messageTextBlock.HorizontalAlignment = HorizontalAlignment.Left;
                    messageTextBlock.TextWrapping = TextWrapping.Wrap;

                    Button okButton = new Button();
                    okButton.Content = "OK";
                    okButton.Margin = new Thickness(0, 0, 20, 10);
                    okButton.Padding = new Thickness(25, 3,25,3);
                    okButton.HorizontalAlignment = HorizontalAlignment.Right;
                    okButton.Click += delegate
                    {
                        popup.IsOpen = false;
                        parent.IsEnabled = true;
                    };

                    headDockPanel.Children.Add(headTextBlock);
                    aroundStackPanel.Children.Add(headDockPanel);
                    aroundStackPanel.Children.Add(messageTextBlock);
                    aroundStackPanel.Children.Add(okButton);
                    aroundBorder.Child = aroundStackPanel;
                    popup.Child = aroundBorder;

                    parent.IsEnabled = false;
                    popup.IsOpen = true;
                }));
        }
    }
}

使用方法:

MessagePopup.Show(this,”显示文字”);

显示效果如下:

Leave a Reply

Your email address will not be published. Required fields are marked *