DataGridTemplateColumn 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表 DataGrid 一個欄位,其儲存格中承載模板指定的內容。
public ref class DataGridTemplateColumn : System::Windows::Controls::DataGridColumn
public class DataGridTemplateColumn : System.Windows.Controls.DataGridColumn
type DataGridTemplateColumn = class
inherit DataGridColumn
Public Class DataGridTemplateColumn
Inherits DataGridColumn
- 繼承
範例
以下範例說明如何建立 DataGridTemplateColumn。
<Grid>
<Grid.Resources>
<!--DataTemplate for Published Date column defined in Grid.Resources. PublishDate is a property on the ItemsSource of type DateTime -->
<DataTemplate x:Key="DateTemplate" >
<StackPanel Width="20" Height="30">
<Border Background="LightBlue" BorderBrush="Black" BorderThickness="1">
<TextBlock Text="{Binding PublishDate, StringFormat={}{0:MMM}}" FontSize="8" HorizontalAlignment="Center" />
</Border>
<Border Background="White" BorderBrush="Black" BorderThickness="1">
<TextBlock Text="{Binding PublishDate, StringFormat={}{0:yyyy}}" FontSize="8" FontWeight="Bold" HorizontalAlignment="Center" />
</Border>
</StackPanel>
</DataTemplate>
<!--DataTemplate for the Published Date column when in edit mode. -->
<DataTemplate x:Key="EditingDateTemplate">
<DatePicker SelectedDate="{Binding PublishDate}" />
</DataTemplate>
</Grid.Resources>
<DataGrid Name="DG1" ItemsSource="{Binding}" AutoGenerateColumns="False" >
<DataGrid.Columns>
<!--Custom column that shows the published date-->
<DataGridTemplateColumn Header="Publish Date" CellTemplate="{StaticResource DateTemplate}" CellEditingTemplate="{StaticResource EditingDateTemplate}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
以下示意圖顯示先前 XAML 的輸出。
備註
這個 DataGridTemplateColumn 型別讓你能透過指定顯示和編輯值時使用的儲存格模板,建立自己的欄位類型。 若要指定用於顯示非編輯模式儲存格內容的範本,請設定屬性 CellTemplate 。 若要指定用於顯示編輯模式儲存格內容的範本,請設定屬性 CellEditingTemplate 。 欲了解更多範本資訊,請參閱 資料模板概述 與 樣式與模板。
為方便起見, DataGrid 提供以下預先定義的欄位類型:
| 欄位類型 | 資料顯示 |
|---|---|
| DataGridHyperlinkColumn | 用來顯示 URI 資料。 |
| DataGridComboBoxColumn | 用來顯示列舉資料。 |
| DataGridTextColumn | 用來顯示文字。 |
| DataGridCheckBoxColumn | 用來顯示布林資料。 |
建構函式
| 名稱 | Description |
|---|---|
| DataGridTemplateColumn() |
初始化 DataGridTemplateColumn 類別的新執行個體。 |
欄位
| 名稱 | Description |
|---|---|
| CellEditingTemplateProperty |
識別 CellEditingTemplate 依賴性質。 |
| CellEditingTemplateSelectorProperty |
識別 CellEditingTemplateSelector 依賴性質。 |
| CellTemplateProperty |
識別 CellTemplate 依賴性質。 |
| CellTemplateSelectorProperty |
識別 CellEditingTemplateSelector 依賴性質。 |
屬性
| 名稱 | Description |
|---|---|
| ActualWidth |
取得該柱目前的寬度,單位與裝置無關(每單位1/96英吋)。 (繼承來源 DataGridColumn) |
| CanUserReorder |
取得或設定一個值,指示使用者是否可以透過拖動欄位標頭來改變欄位顯示位置。 (繼承來源 DataGridColumn) |
| CanUserResize |
取得或設定一個值,指示使用者是否能透過滑鼠調整欄位寬度。 (繼承來源 DataGridColumn) |
| CanUserSort |
取得或設定一個值,指示使用者是否能透過點擊欄位標頭來排序該欄位。 (繼承來源 DataGridColumn) |
| CellEditingTemplate |
取得或設定範本,用來顯示處於編輯模式的儲存格內容。 |
| CellEditingTemplateSelector |
取得或設定物件,決定使用哪個範本來顯示處於編輯模式的儲存格內容。 |
| CellStyle |
取得或設定用於渲染欄位中儲存格的樣式。 (繼承來源 DataGridColumn) |
| CellTemplate |
取得或設定範本,用來顯示非編輯模式儲存格的內容。 |
| CellTemplateSelector |
取得或設定物件,決定使用哪個範本來顯示非編輯模式的儲存格內容。 |
| ClipboardContentBinding |
取得或設定綁定物件,以便在取得或設定剪貼簿的儲存格內容時使用。 (繼承來源 DataGridColumn) |
| DataGridOwner |
取得 DataGrid 包含此欄位的控制項。 (繼承來源 DataGridColumn) |
| DependencyObjectType |
會取得 DependencyObjectType 包裹此實例 CLR 類型的 。 (繼承來源 DependencyObject) |
| Dispatcher |
了解 Dispatcher 這與此 DispatcherObject 有關。 (繼承來源 DispatcherObject) |
| DisplayIndex |
取得或設定該欄位相對於其他欄位 DataGrid的顯示位置。 (繼承來源 DataGridColumn) |
| DragIndicatorStyle |
在拖曳操作中取得或設定樣式物件套用到欄位標頭。 (繼承來源 DataGridColumn) |
| 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) |
| Visibility |
取得或設定欄位的可見性。 (繼承來源 DataGridColumn) |
| Width |
可以設定欄位寬度或自動尺寸模式。 (繼承來源 DataGridColumn) |
方法
事件
| 名稱 | Description |
|---|---|
| CopyingCellClipboardContent |
發生在儲存區剪貼板內容準備完成後。 (繼承來源 DataGridColumn) |
| PastingCellClipboardContent |
發生在剪貼簿內容移動到儲存格之前。 (繼承來源 DataGridColumn) |