封装了Popup

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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,”显示文字”);

显示效果如下: