2010-11-05 960 views

回答

25

試試這個

<Style x:Key="DataGridCellStyle" TargetType="{x:Type DataGridCell}" > 
    <Style.Triggers> 
     <Trigger Property="IsSelected" Value="True"> 
      <Setter Property="Foreground" Value="Green"/> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

然後你就可以在你看到的列使用它適合像

<DataGrid ...> 
    <DataGrid.Columns> 
     <DataGridTextColumn CellStyle="{StaticResource DataGridCellStyle}" .../> 

如果你想讓它適用於所有列,你可以改變X:關鍵樣式爲

<Style x:Key="{x:Type DataGridCell}" TargetType="{x:Type DataGridCell}" > 
+0

謝謝 - 這工程! – bplus 2010-11-05 10:25:12

0

如果要完全刪除前景色的變化(比方說,如果你的DataGrid有不同的變化顏色不同行),你可以這樣做:

<Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {x:Type DataGridCell}}"> 
     <Style.Triggers> 
      <Trigger Property="IsSelected" Value="True"> 
       <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource Self}, Path=Foreground}" /> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 

如果你想給這個樣式的名稱,像前面的回答,添加X:關鍵。