wpf中利用代码完成自定义tooltip
没啥可说的,简单的代码如下:
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Windows;
6using System.Windows.Controls;
7using System.Windows.Data;
8using System.Windows.Documents;
9using System.Windows.Input;
10using System.Windows.Media;
11using System.Windows.Media.Imaging;
12using System.Windows.Navigation;
13using System.Windows.Shapes;
14
15namespace tooltip
16{
17 /// <summary>
18 /// Window1.xaml 的交互逻辑
19 /// </summary>
20 public partial class Window1 : Window
21 {
22 public Window1()
23 {
24 InitializeComponent();
25 button1.ToolTip = GetToolTip("11", "22");
26 }
27 private ToolTip GetToolTip(string name, string remark)
28 {
29
30
31
32 StackPanel sp = new StackPanel();
33
34 Label lb = new Label();
35 lb.FontWeight = FontWeights.Bold;
36 lb.Background = Brushes.Blue;
37 lb.Foreground = Brushes.White;
38 lb.Content = name + "检测结果";
39 sp.Children.Add(lb);
40
41 TextBlock tb = new TextBlock();
42 tb.Padding = new Thickness(10);
43 tb.TextWrapping = TextWrapping.WrapWithOverflow;
44 tb.Width = 200;
45 tb.Text = name + "\r" + remark;
46 sp.Children.Add(tb);
47
48 Line ln = new Line();
49 ln.Stroke = Brushes.Black;
50 ln.StrokeThickness = 1;
51 ln.X2 = 200;
52 sp.Children.Add(ln);
53
54 StackPanel sp2 = new StackPanel();
55 sp2.Orientation = Orientation.Horizontal;
56
57 //-------------------sp2中增加一张图片----------------------
58
59 Image myImage3 = new Image();
60 BitmapImage bi3 = new BitmapImage();
61 bi3.BeginInit();
62 bi3.UriSource = new Uri("images/help.jpg", UriKind.Relative);
63 bi3.EndInit();
64 myImage3.Stretch = Stretch.Fill;
65 myImage3.Source = bi3;
66 sp2.Children.Add(myImage3);
67
68 //-----------------------------------------------------------
69
70 Label lb2 = new Label();
71 lb2.FontWeight = FontWeights.Bold;
72 lb2.Content = "右键菜单显示路由信息";
73 sp2.Children.Add(lb2);
74
75 sp.Children.Add(sp2);
76
77 ToolTip ttp = new ToolTip();
78
79 ttp.Content = sp;
80
81
82 return (ttp);
83 }
84 }
85}
86
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Windows;
6using System.Windows.Controls;
7using System.Windows.Data;
8using System.Windows.Documents;
9using System.Windows.Input;
10using System.Windows.Media;
11using System.Windows.Media.Imaging;
12using System.Windows.Navigation;
13using System.Windows.Shapes;
14
15namespace tooltip
16{
17 /// <summary>
18 /// Window1.xaml 的交互逻辑
19 /// </summary>
20 public partial class Window1 : Window
21 {
22 public Window1()
23 {
24 InitializeComponent();
25 button1.ToolTip = GetToolTip("11", "22");
26 }
27 private ToolTip GetToolTip(string name, string remark)
28 {
29
30
31
32 StackPanel sp = new StackPanel();
33
34 Label lb = new Label();
35 lb.FontWeight = FontWeights.Bold;
36 lb.Background = Brushes.Blue;
37 lb.Foreground = Brushes.White;
38 lb.Content = name + "检测结果";
39 sp.Children.Add(lb);
40
41 TextBlock tb = new TextBlock();
42 tb.Padding = new Thickness(10);
43 tb.TextWrapping = TextWrapping.WrapWithOverflow;
44 tb.Width = 200;
45 tb.Text = name + "\r" + remark;
46 sp.Children.Add(tb);
47
48 Line ln = new Line();
49 ln.Stroke = Brushes.Black;
50 ln.StrokeThickness = 1;
51 ln.X2 = 200;
52 sp.Children.Add(ln);
53
54 StackPanel sp2 = new StackPanel();
55 sp2.Orientation = Orientation.Horizontal;
56
57 //-------------------sp2中增加一张图片----------------------
58
59 Image myImage3 = new Image();
60 BitmapImage bi3 = new BitmapImage();
61 bi3.BeginInit();
62 bi3.UriSource = new Uri("images/help.jpg", UriKind.Relative);
63 bi3.EndInit();
64 myImage3.Stretch = Stretch.Fill;
65 myImage3.Source = bi3;
66 sp2.Children.Add(myImage3);
67
68 //-----------------------------------------------------------
69
70 Label lb2 = new Label();
71 lb2.FontWeight = FontWeights.Bold;
72 lb2.Content = "右键菜单显示路由信息";
73 sp2.Children.Add(lb2);
74
75 sp.Children.Add(sp2);
76
77 ToolTip ttp = new ToolTip();
78
79 ttp.Content = sp;
80
81
82 return (ttp);
83 }
84 }
85}
86