2014-02-24 206 views
4

我有以下樣式WPF DataGrid的風格

<Style x:Key="DataGridRowStyle1" TargetType="{x:Type DataGridRow}"> 
    <Setter Property="Foreground" Value="#FFB3B3B3"/> 
    <Setter Property="Height" Value="25"/> 
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 
    <Setter Property="Template" Value="{DynamicResource DataGridRowControlTemplate1}"/> 
    <Style.Triggers> 
     <Trigger Property="IsSelected" Value="True"> 
      <Setter Property="Background" Value="#FF262626"/> 
     </Trigger> 
     <Trigger Property="ItemsControl.AlternationIndex" Value="0"> 
      <Setter Property="Background" Value="#FF383838"/> 
     </Trigger> 
     <Trigger Property="ItemsControl.AlternationIndex" Value="1"> 
      <Setter Property="Background" Value="#FF333333"/> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

一個DataGrid,它似乎是這樣的:

enter image description here

在DataGrid失去焦點似乎我的問題:

enter image description here

我該如何讓我t的外觀與焦點無關嗎?

+0

可以共享完成全XAML的風格?看起來不錯。 – bashkan

+0

我可以問你正在使用哪種字體嗎? –

+0

我不記得了,對不起 –

回答

2

在嘗試查找解決方案之前,請查看DataGrid,DataGridRow等的Style/Template等Focus on Focus觸發器(IsFocused),因爲它不能作爲默認行爲。

如果沒有一個,嘗試添加EvenTriggers事件GotFocusLostFocus這樣的:

<Window.Resources> 
    <SolidColorBrush x:Key="GotFocusColor" Color="Green" /> 
    <SolidColorBrush x:Key="LostFocusColor" Color="Transparent" /> 

    <Style TargetType="{x:Type DataGridRow}"> 
     <Setter Property="Foreground" Value="#FFB3B3B3"/> 
     <Setter Property="Height" Value="25"/> 
     <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 

     <Style.Triggers> 
      <Trigger Property="IsSelected" Value="True"> 
       <Setter Property="Background" Value="#FF262626"/> 
      </Trigger> 

      <Trigger Property="ItemsControl.AlternationIndex" Value="0"> 
       <Setter Property="Background" Value="#FF383838"/> 
      </Trigger> 

      <Trigger Property="ItemsControl.AlternationIndex" Value="1"> 
       <Setter Property="Background" Value="#FF333333"/> 
      </Trigger> 

      <EventTrigger RoutedEvent="DataGrid.GotFocus"> 
       <BeginStoryboard> 
        <Storyboard> 
         <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background"> 
          <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource GotFocusColor}" /> 
         </ObjectAnimationUsingKeyFrames> 
        </Storyboard> 
       </BeginStoryboard> 
      </EventTrigger> 

      <EventTrigger RoutedEvent="DataGrid.LostFocus"> 
       <BeginStoryboard> 
        <Storyboard> 
         <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background"> 
          <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource LostFocusColor}" /> 
         </ObjectAnimationUsingKeyFrames> 
        </Storyboard> 
       </BeginStoryboard> 
      </EventTrigger> 
     </Style.Triggers> 
    </Style> 
</Window.Resources> 
+1

一個人應該測試你的解決方案並給我一個反饋!對推遲感到抱歉。 –

+0

@ m.samy:我的回答對你有幫助,還是你有問題? –

+0

感謝此工作的WPF MUI應用程序內的數據網格,但只是將Windows.Resource更改爲UserControl.Resource,因爲這是在UserControl – BENN1TH