2012-11-17 172 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> 

希望它可以帮助别人!