2009-10-21 34 views
1

用戶界面控件的正常慣例是當父控件處於焦點時,所選文本,項目或行的顏色鮮豔(例如,明亮的藍色),而當控件不在時,該控件處於去飽和/昏暗(例如,淡藍色)焦點。DataGrid不在焦點時,如何使所選行飽和?

ListView/GridView不同,WPF DataGrid控件默認不遵循此約定。即使在同一個窗口中的另一個控件清晰地對焦,選定的行也會顯示爲明亮。

我想,這應該只是增加一個觸發器,設置的背景{DynamicResource {x:Static SystemColors.ControlBrushKey}}如果DataGrid是失焦的DataGridCell的問題,但我想不出來檢查其屬性。 IsFocused聽起來像我想要的,但那不起作用。 IsMouseCaptured似乎也沒有工作。

這是我試過的最新觸發器:

<MultiDataTrigger> 
    <MultiDataTrigger.Conditions> 
     <Condition 
      Binding="{Binding IsSelected}" 
      Value="True" /> 
     <Condition 
      Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=wt:DataGrid}, Path=IsFocused}" 
      Value="False"/> 
    </MultiDataTrigger.Conditions> 
    <Setter 
     Property="BorderBrush" 
     Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" /> 
    <Setter 
     Property="Background" 
     Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" /> 
    <Setter 
     Property="Foreground" 
     Value="Gray" /> 
</MultiDataTrigger> 

我能做些什麼,使這個觸發暗淡單元格時被選中的單元格,父控件沒有焦點?

回答

2

嗯,這是一個龐然大物,但它似乎工作:

<MultiDataTrigger> 
    <MultiDataTrigger.Conditions> 
     <Condition 
      Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=wt:DataGridRow}, Path=IsSelected}" 
      Value="True" /> 
     <Condition 
      Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=wt:DataGrid}, Path=IsKeyboardFocusWithin}" 
      Value="True" /> 
    </MultiDataTrigger.Conditions> 
    <Setter 
     Property="BorderBrush" 
     Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" /> 
    <Setter 
     Property="Background" 
     Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" /> 
    <Setter 
     Property="Foreground" 
     Value="Black" /> 
</MultiDataTrigger> 
<MultiDataTrigger> 
    <MultiDataTrigger.Conditions> 
     <Condition 
      Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=wt:DataGridRow}, Path=IsSelected}" 
      Value="True" /> 
     <Condition 
      Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=wt:DataGrid}, Path=IsKeyboardFocusWithin}" 
      Value="False" /> 
    </MultiDataTrigger.Conditions> 
    <Setter 
     Property="BorderBrush" 
     Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" /> 
    <Setter 
     Property="Background" 
     Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" /> 
    <Setter 
     Property="Foreground" 
     Value="Gray" /> 
</MultiDataTrigger> 
+0

'wt:'命名空間是什麼? – Tower 2012-05-27 10:41:18

0

從我的一些代碼中拉出來:這是treeview項目背景在樹視圖失焦時的觸發器。

     <MultiTrigger> 
          <MultiTrigger.Conditions> 
           <Condition Property="IsSelected" Value="true"/> 
           <Condition Property="IsSelectionActive" Value="false"/> 
          </MultiTrigger.Conditions> 
          <Setter Property="Background" TargetName="SelectBorder"> 
           <Setter.Value> 
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
             <GradientStop Color="#939F90" Offset="0"/> 
             <GradientStop Color="#BCC4BA" Offset="1"/> 
            </LinearGradientBrush> 
           </Setter.Value> 
          </Setter> 
          <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
         </MultiTrigger> 

相信IsSelectionActive是關鍵條件...

+0

@AP,感謝您的想法,但我得到一個編譯器錯誤(「無法找到樣式屬性'IsSelectionActive'」)。 – devuxer 2009-10-21 01:24:23

+0

lemme多挖一些,看看還有什麼東西在浮動 – 2009-10-21 01:30:45

+0

我想出了一個解決方案(看我的答案),似乎工作。再次感謝你的幫助。 – devuxer 2009-10-21 02:04:25

相關問題