2015-10-20 86 views
2

我有這個文本塊默認前景色爲白色顏色樣式文本塊

<TextBlock Text="First Cmd" Grid.Row="0" TextAlignment="Center" Margin="4" TextWrapping="Wrap" Foreground="White" Style="{DynamicResource ABC}"> 
     <TextBlock.InputBindings> 
       <MouseBinding Command="{Binding AAA}" MouseAction="LeftClick" /> 
     </TextBlock.InputBindings> 
</TextBlock> 

當鼠標在文本塊,於地面顏色要黑的變化,但這種風格不起作用。爲什麼?

<Style x:Key="ABC" TargetType="{x:Type TextBlock}"> 
    <Style.Triggers> 
     <Trigger Property ="IsMouseOver" Value="True"> 
      <Setter Property= "Foreground" Value="Black"> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

回答

3

你爲TextBlock設置Foreground在本地,所以Trigger二傳手不能覆蓋。你需要使用一個樣式setter方法來設置初始前景:

<Style x:Key="ABC" TargetType="{x:Type TextBlock}"> 
    <Setter Property="Foreground" Value="White"/> 
    <Style.Triggers> 
    <Trigger Property ="IsMouseOver" Value="True"> 
     <Setter Property= "Foreground" Value="Black"> 
    </Trigger> 
    </Style.Triggers> 
</Style> 

Foreground="White"應該從<TextBlock ...被刪除。

瞭解有關Dependency Property Value Precedence的更多信息。

+1

好.. !!! ;-) 非常感謝你 !!! – Alan392