2014-11-20 110 views
6

我有一個WPF DataGrid的SelectedItem綁定到ViewModel屬性。WPF DataGrid - 高亮選中的行,即使SelectedItem是綁定屬性

SelectedItem="{Binding DataContext.SelectedBooking, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" 

如果用戶點擊某一行上,選擇它,唯一的視覺線索是該行的灰色背景變得非常稍輕。我需要使這更明顯,所以我嘗試添加這些個別:

<DataGrid.RowStyle> 
    <Style TargetType="{x:Type DataGridRow}"> 
     <Style.Triggers> 
      <Trigger Property="IsSelected" Value="True"> 
       <Setter Property="Foreground" Value="Red"/> 
       <Setter Property="Background" Value="Red" /> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
</DataGrid.RowStyle> 

而且

<DataGrid.Resources> 
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/> 
</DataGrid.Resources> 

的結果是一樣的。當用戶單擊一行時,它會非常短暫地閃爍紅色,然後回到淺灰色,儘管該行實際上保持爲選中狀態。如果他們再次點擊它,它會變紅並保持紅色。

如果我刪除了SelectedItem上的綁定,它按預期工作。無論綁定如何,我都可以做這項工作?

回答

7

我自己找到答案,滾動瀏覽SystemColors的Intellisense。還有一個InactiveSelection畫筆也可以覆蓋。

<DataGrid.Resources> 
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/> 
    <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Red"/> 
</DataGrid.Resources> 
+1

一個有趣的小黑客在這裏,你可以做的是也:'<的SolidColorBrush X:鍵= 「{X:靜態SystemColors.InactiveSelectionHighlightBrushKey}」 顏色= 「{DynamicResource {X:靜態SystemColors.HighlightColorKey}}」 />'這樣可以使非活動狀態設置爲活動狀態,無論是默認還是必須更改高亮鍵。 – 2016-05-09 18:07:37

+0

InactiveSelectionHighlightBrushKey僅適用於FW 4.5,並引發FW 4.0的異常。請檢查http://stackoverflow.com/a/13827971/1815957 – diedie2 2016-09-22 11:31:00

相關問題