• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
挂在墙上的天堂
属于我的蓝白黑
博客园    首页    新随笔    联系   管理    订阅  订阅

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

 

posted @ 2008-08-30 09:47  卢巍  阅读(4364)  评论(6)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3