
- WPF 教程
- WPF - 首頁
- WPF - 概述
- WPF - 環境設定
- WPF - Hello World
- WPF - XAML 概述
- WPF - 元素樹
- WPF - 依賴屬性
- WPF - 路由事件
- WPF - 控制元件
- WPF - 佈局
- WPF - 佈局巢狀
- WPF - 輸入
- WPF - 命令列
- WPF - 資料繫結
- WPF - 資源
- WPF - 模板
- WPF - 樣式
- WPF - 觸發器
- WPF - 除錯
- WPF - 自定義控制元件
- WPF - 異常處理
- WPF - 本地化
- WPF - 互動
- WPF - 2D 圖形
- WPF - 3D 圖形
- WPF - 多媒體
- WPF 有用資源
- WPF - 快速指南
- WPF - 有用資源
- WPF - 討論
WPF - 工具提示
工具提示是一個控制元件,它建立一個彈出視窗,用於顯示 GUI 中元素的資訊。ToolTip 類的繼承層次結構如下:

ToolTip 類常用屬性
序號 | 屬性及說明 |
---|---|
1 | IsOpen 獲取或設定一個值,該值指示 ToolTip 是否可見。 |
2 | IsOpenProperty 標識 IsOpen 依賴屬性。 |
3 | Placement 獲取或設定 ToolTip 相對於放置目標元素的位置。 |
4 | PlacementProperty 標識 Placement 依賴屬性。 |
5 | PlacementTarget 獲取或設定 ToolTipService 開啟時,工具提示應相對於其定位的視覺元素或控制元件。 |
6 | PlacementTargetProperty 標識 PlacementTarget 依賴屬性。 |
7 | TemplateSettings 獲取一個物件,該物件提供可以作為 TemplateBinding 源引用的計算值,用於定義 ToolTip 的模板。 |
ToolTip 類常用事件
序號 | 事件及說明 |
---|---|
1 | Closed 當 ToolTip 關閉且不再可見時發生。 |
2 | Opened 當 ToolTip 變為可見時發生。 |
示例
讓我們建立一個名為 **WPFToolTipControl** 的新 WPF 專案。
從工具箱中拖動兩個文字塊、兩個文字框和一個按鈕。
以下示例演示如何在 WPF 應用程式中使用 ToolTip。
以下 XAML 程式碼建立了一個帶有某些屬性的 ToolTip,用於在按鈕和文字框上顯示工具提示。
<Window x:Class = "WPFToolTipControl.MainWindow" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local = "clr-namespace:WPFToolTipControl" mc:Ignorable = "d" Title = "MainWindow" Height = "350" Width = "604"> <Grid> <TextBlock x:Name = "textBlock" HorizontalAlignment = "Left" Margin = "101,75,0,0" TextWrapping = "Wrap" Text = "User Name" VerticalAlignment = "Top" /> <TextBlock x:Name = "textBlock1" HorizontalAlignment = "Left" Margin = "101,125,0,0" TextWrapping = "Wrap" Text = "Password" VerticalAlignment = "Top" /> <TextBox x:Name = "textBox" HorizontalAlignment = "Left" Height = "24" Margin = "199,75,0,0" TextWrapping = "Wrap" VerticalAlignment = "Top" Width = "219" ToolTipService.ToolTip = "Enter User Name" /> <PasswordBox x:Name = "passwordBox" HorizontalAlignment = "Left" Margin = "199,125,0,0" VerticalAlignment = "Top" Width = "219" Height = "24" ToolTipService.ToolTip = "Enter Password" /> <Button x:Name = "button" Content = "Log in" HorizontalAlignment = "Left" Margin = "199,189,0,0" VerticalAlignment = "Top" Width = "75" ToolTipService.ToolTip = "Log in" /> </Grid> </Window>
編譯並執行上述程式碼後,將產生以下輸出。當滑鼠進入按鈕或文字框區域時,將顯示工具提示。

我們建議您執行上述示例程式碼,並嘗試 ToolTip 類的其他屬性和事件。
wpf_controls.htm
廣告