2017-03-16 121 views
1

如何在我的行中保留文本,例如當DataGrid行被選中時爲白色,以及當它處於非活動選擇模式時(它被選中現在用戶點擊另一個控件即文本框)。當選擇單元格/行時更改單元格/行的文本顏色 - DataGrid WPF

我試着用這個(設置單元格樣式):

<DataGrid.CellStyle> 
    <StaticResource ResourceKey="DataGridCentering"/> 
</DataGrid.CellStyle> 

如果我在App.xaml中說:

<Style x:Key="DataGridCentering" TargetType="{x:Type DataGridCell}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type DataGridCell}"> 
       <Grid Background="{TemplateBinding Background}"> 
        <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" /> 
       </Grid> 
       </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/> 
    <Style.Triggers> 
     <Trigger Property="IsSelected" Value="True"> 
      <Setter Property="Foreground" Value="White"/> 
     </Trigger> 
     </Style.Triggers> 
</Style> 

由於可以注意到,我試圖做到這一點的觸發器,即當單元格被選中顏色我的文本內的單元格與白色等, 但遺忘地這是行不通的

當我選擇單元格/行時DataGrid中的文本仍然是黑色的..

+0

[設置選中行時WPF DataGrid行的文本顏色]的可能重複(http://stackoverflow.com/questions/4104646/setting-the-text-colour-of-a-wpf-datagrid- row-when-row-is-selected) – AlSki

回答

1

嘗試以下刷資源添加到DataGrid

<DataGrid> 
    <DataGrid.Resources> 
     <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="White"/> 
     <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="White"/> 
    </DataGrid.Resources> 
    ... 
</DataGrid> 

應該在Windows 7上運行在Windows 8和以後你會必須覆蓋DataGridRow的控制模板。

+1

比它可能會更好地覆蓋它現在的情況下,如果它將用於其他操作系統的......我真的想知道如何觸發器不起作用,我想他們必須工作100%! –

+0

他們工作;)看看我的答案。 –

1

嘗試這種風格

<DataGrid.Resources> 
    <Style TargetType="{x:Type DataGridCell}"> 
     <Style.Triggers> 
      <Trigger Property="IsSelected" 
        Value="True"> 
       <Setter Property="Foreground" 
         Value="Red" /> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
</DataGrid.Resources> 
相關問題