2012-11-17 86 views
4

我已經與DataGrid發生了非常奇怪的行爲。我在DataGridRow上觸發以下代碼wpf DataGrid失去焦點時丟失行選擇

<Style TargetType="{x:Type DataGridRow}"> 
    <Style.Triggers> 
     <Trigger Property="IsSelected" Value="True"> 
      <Setter Property="Background" Value="{StaticResource SelectionBackgroundBrush}"/> 
      <Setter Property="Foreground" Value="White"/> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

最初,當選中該行時,我從上述觸發器中獲取行爲。但是,選擇後,如果DataGrid失去焦點(例如,我單擊窗口上的某個其他按鈕),Foreground屬性會丟失其值,但背景保持與觸發器中指定的相同。有沒有人來過這種行爲,或者上面的代碼存在一些問題(或者在我的應用程序中的其他地方)。上述問題的任何解決方法?

回答

0

我用DataGridCell代替DataGridRow和它的作品對我來說

<Style TargetType="{x:Type DataGridCell}"> 
     <Style.Triggers> 
      <Trigger Property="DataGridCell.IsSelected" Value="True"> 
       <Setter Property="BorderBrush" Value="#CCDAFF" /> 
       <Setter Property="Background" Value="#3399ff"/> 
       <Setter Property="Foreground" Value="White"/> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 

希望它可以幫助別人!