DataGridHyperlinkColumn 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
public ref class DataGridHyperlinkColumn : System::Windows::Controls::DataGridBoundColumn
public class DataGridHyperlinkColumn : System.Windows.Controls.DataGridBoundColumn
type DataGridHyperlinkColumn = class
inherit DataGridBoundColumn
Public Class DataGridHyperlinkColumn
Inherits DataGridBoundColumn
- 繼承
範例
下列範例示範使用 DataGridHyperlinkColumnUri 屬性設定 的 Binding ,以及具有 ContentBinding 屬性的超連結文字。 事件 Hyperlink.Click 會自動處理,因為 NavigationWindow 是 的 DataGrid 父系。
<NavigationWindow x:Class="DataGrid_CustomColumns.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:core="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:DataGrid_CustomColumns"
Title="Customers" Height="300" Width="300" ShowsNavigationUI="False" >
<NavigationWindow.Resources>
<!--Create an instance of the converter for Email-->
<local:EmailConverter x:Key="EmailConverter" />
</NavigationWindow.Resources>
<NavigationWindow.Content>
<Grid>
<DataGrid Name="DG1" ItemsSource="{Binding}" AutoGenerateColumns="False" >
<DataGrid.Columns>
<!--The Email property contains a URI. For example "mailto:lucy0@adventure-works.com"-->
<DataGridHyperlinkColumn Header="Email" Binding="{Binding Email}" ContentBinding="{Binding Email, Converter={StaticResource EmailConverter}}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</NavigationWindow.Content>
</NavigationWindow>
public partial class Window1 : NavigationWindow
{
Class Window1
public Window1()
{
InitializeComponent();
//GetData() creates a collection of Customer data from a database
ObservableCollection<Customer> custdata = GetData();
//Bind the DataGrid to the customer data
DG1.DataContext = custdata;
}
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
'GetData() creates a collection of Customer data from a database
Dim custdata As ObservableCollection(Of Customer) = GetData()
'Bind the DataGrid to the customer data
DG1.DataContext = custdata
End Sub
//Defines the customer object
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Uri Email { get; set; }
public bool IsMember { get; set; }
public OrderStatus Status { get; set; }
}
'Defines the customer object
Public Class Customer
Public Property FirstName() As String
Public Property LastName() As String
Public Property Email() As Uri
Public Property IsMember() As Boolean
Public Property Status() As OrderStatus
End Class
}
End Class
//Converts the mailto uri to a string with just the customer alias
public class EmailConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null)
{
string email = value.ToString();
int index = email.IndexOf("@");
string alias = email.Substring(7, index-7);
return alias;
}
else
{
string email = "";
return email;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Uri email = new Uri((string)value);
return email;
}
}
'Converts the mailto uri to a string with just the customer alias
Public Class EmailConverter
Implements IValueConverter
Public Function Convert(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
If value IsNot Nothing Then
Dim email As String = value.ToString()
Dim index As Integer = email.IndexOf("@")
Dim [alias] As String = email.Substring(7, index - 7)
Return [alias]
Else
Dim email As String = ""
Return email
End If
End Function
Public Function ConvertBack(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
Dim email As New Uri(DirectCast(value, String))
Return email
End Function
End Class
備註
用來 DataGridHyperlinkColumn 顯示包含 Uri 的資料,例如 HTTP 位址或電子郵件地址。 下圖顯示 的 DataGridHyperlinkColumn 範例。
注意
Hyperlink 只有在 的直接或間接父 Hyperlink 代是導覽主機時,才能進行導覽。 流覽主機的範例包括 NavigationWindow 、 Frame 或任何可裝載 XBAP 的瀏覽器,例如 Microsoft Internet Explorer 6 或更新版本和 Firefox 2.0 或更新版本。 如需詳細資訊,請參閱 導覽概觀中的主題。
若要填入資料行,請使用 Binding 屬性將資料行系結至 Uri 資料。 若要顯示與 URI 字串不同的超連結文字,請將文字值系結至 ContentBinding 屬性。 屬性 Binding 會套用至 Hyperlink 資料行中建立的 或 TextBox 專案。 DataContext每個資料格中元素的 是儲存格所在的資料列資料項目。 因此,若要設定系結,您只需要設定 Binding.Path 。 或者,如果您想要轉換資料,可以指定 Binding.Converter 。 如需資料系結的詳細資訊,請參閱資料系結 (WPF) 。
注意
DataGridHyperlinkColumn 在 Hyperlink 非編輯模式中建立專案,以及在編輯模式中建立 TextBox 專案。
如果您想要顯示其他類型的資料, DataGrid 請提供下列資料行類型:
資料行類型 | 資料顯示 |
---|---|
DataGridCheckBoxColumn | 用來顯示布林資料。 |
DataGridComboBoxColumn | 用來顯示列舉資料。 |
DataGridTextColumn | 使用 來顯示文字。 |
如果您想要在 中使用其他控制項 DataGrid ,您可以使用 來建立自己的資料行類型 DataGridTemplateColumn 。
建構函式
DataGridHyperlinkColumn() |
初始化 DataGridHyperlinkColumn 類別的新執行個體。 |
欄位
TargetNameProperty |
識別 TargetName 相依性屬性。 |
屬性
ActualWidth |
取得資料行的目前寬度,以裝置獨立單位 (為單位,每單位 1/96 英吋) 。 (繼承來源 DataGridColumn) |
Binding |
取得或設定可產生欄與資料來源中屬性之關聯的繫結。 (繼承來源 DataGridBoundColumn) |
CanUserReorder |
取得或設定值,該值指出使用者是否可藉由拖曳欄標題來變更欄顯示位置。 (繼承來源 DataGridColumn) |
CanUserResize |
取得或設定值,這個值表示使用者是否可以使用滑鼠來調整資料行寬度。 (繼承來源 DataGridColumn) |
CanUserSort |
取得或設定值,該值指出使用者是否可藉由按一下欄標題來排序欄。 (繼承來源 DataGridColumn) |
CellStyle |
取得或設定用於呈現資料行中儲存格的樣式。 (繼承來源 DataGridColumn) |
ClipboardContentBinding |
取得或設定當取得或設定剪貼簿的儲存格內容時,要使用的繫結物件。 (繼承來源 DataGridBoundColumn) |
ContentBinding |
取得或設定超連結的文字繫結。 |
DataGridOwner |
取得包含此資料行的 DataGrid 控制項。 (繼承來源 DataGridColumn) |
DefaultEditingElementStyle |
EditingElementStyle 屬性的預設值。 |
DefaultElementStyle |
ElementStyle 屬性的預設值。 |
DependencyObjectType |
DependencyObjectType取得包裝這個實例之 CLR 型別的 。 (繼承來源 DependencyObject) |
Dispatcher |
取得與這個 Dispatcher 關聯的 DispatcherObject。 (繼承來源 DispatcherObject) |
DisplayIndex |
取得或設定相對於 DataGrid 中之其他資料行的資料行位置。 (繼承來源 DataGridColumn) |
DragIndicatorStyle |
取得或設定在拖曳作業期間要套用至資料行標題的樣式物件。 (繼承來源 DataGridColumn) |
EditingElementStyle |
取得或設定樣式,當呈現資料行針對處於編輯模式之儲存格所顯示的項目時,會使用這個樣式。 (繼承來源 DataGridBoundColumn) |
ElementStyle |
取得或設定樣式,當呈現資料行針對不處於編輯模式之儲存格所顯示的項目時,會使用這個樣式。 (繼承來源 DataGridBoundColumn) |
Header |
取得或設定欄標題的內容。 (繼承來源 DataGridColumn) |
HeaderStringFormat |
取得或設定格式模式,以套用到欄標題的內容。 (繼承來源 DataGridColumn) |
HeaderStyle |
取得或設定呈現欄標題時所用的樣式。 (繼承來源 DataGridColumn) |
HeaderTemplate |
取得或設定範本,以定義欄標題的視覺表示方式。 (繼承來源 DataGridColumn) |
HeaderTemplateSelector |
取得或設定物件,以選取要用於欄標題的範本。 (繼承來源 DataGridColumn) |
IsAutoGenerated |
取得值,這個值會指出資料行是否自動產生。 (繼承來源 DataGridColumn) |
IsFrozen |
取得值,這個值會指出資料行是否會避免水平捲動。 (繼承來源 DataGridColumn) |
IsReadOnly |
取得或設定值,該值指出是否可編輯欄中的儲存格。 (繼承來源 DataGridColumn) |
IsSealed |
取得值,這個值表示此執行個體目前是否已密封 (唯讀)。 (繼承來源 DependencyObject) |
MaxWidth |
取得或設定資料行的最大寬度條件約束。 (繼承來源 DataGridColumn) |
MinWidth |
取得或設定資料行的最小寬度條件約束。 (繼承來源 DataGridColumn) |
SortDirection |
取得或設定欄的排序方向 (遞增或遞減)。 (繼承來源 DataGridColumn) |
SortMemberPath |
取得或設定屬性名稱或以句號分隔的屬性名稱階層,要當做排序依據成員。 (繼承來源 DataGridColumn) |
TargetName |
取得或設定超連結的目標視窗或框架名稱。 |
Visibility |
取得或設定欄的可見性。 (繼承來源 DataGridColumn) |
Width |
取得或設定欄寬或自動調整模式。 (繼承來源 DataGridColumn) |
方法
事件
CopyingCellClipboardContent |
發生於準備好儲存格剪貼簿內容之後。 (繼承來源 DataGridColumn) |
PastingCellClipboardContent |
發生在將 [剪貼簿] 內容移至儲存格之前。 (繼承來源 DataGridColumn) |
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應