DataGrid.CellStyle 屬性

定義

取得或設定套用至 DataGrid 中所有儲存格的樣式。

public:
 property System::Windows::Style ^ CellStyle { System::Windows::Style ^ get(); void set(System::Windows::Style ^ value); };
public System.Windows.Style CellStyle { get; set; }
member this.CellStyle : System.Windows.Style with get, set
Public Property CellStyle As Style

屬性值

Style

套用至 DataGrid 中儲存格的樣式。 已註冊的預設值是 null。 如需哪些因素會影響值的詳細資訊,請參閱 DependencyProperty

範例

下列範例會使用觸發程式,在選取儲存格時變更 BackgroundDataGridCell 色彩。

<DataGrid Name="DG1" ItemsSource="{Binding}" SelectionUnit="Cell" >
    <DataGrid.CellStyle>
        <Style TargetType="DataGridCell" >
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="SeaGreen"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </DataGrid.CellStyle>
</DataGrid>

下圖顯示輸出。

綠色背景的已選儲存格

備註

套用 以 Style 更新 中 DataGrid 所有儲存格的視覺外觀。 若要定義 Style 儲存格的 ,請指定 TargetTypeDataGridCell

Style可以套用至資料表、資料行或儲存格層級的儲存格。 若要將 套用 Style 至資料行中的所有儲存格,請設定 DataGridColumn.CellStyle 屬性。 這會優先于 DataGrid.CellStyle 屬性。 若要將 套用 Style 至個別儲存格,請 Style 直接在 上 DataGridCell 設定 屬性。 這會優先于套用至儲存格的所有其他樣式。

適用於

另請參閱