2017-07-21 25 views
0

我的要求是如果網格單元的背景顏色爲紅色,則將前景顏色更改爲白色。WPF:DataTrigger不在DataTemplate內部觸發

我正在使用下面的單元格模板。但是,它沒有觸發。

<DataTemplate x:Key="NumericThreeDecimalCellTemplate"> 
    <TextBlock HorizontalAlignment="Right" 
       VerticalAlignment="Center"> 
     <TextBlock.Text> 
      <Binding Path="Value" 
       StringFormat="###,###,###,###,###,###,##0.000;(###,###,###,###,###,###,##0.000)" /> 
     </TextBlock.Text> 
     <TextBlock.Foreground> 
      <Binding Path="Value" 
        Converter="{StaticResource negativeToBrushConvertor}" /> 
     </TextBlock.Foreground> 


    </TextBlock> 

    <DataTemplate.Resources> 
     <Style TargetType="{x:Type TextBlock}"> 
      <Style.Triggers>      
       <DataTrigger Binding="{Binding Path=Background.Color,RelativeSource={RelativeSource Mode=Self}}" Value="#FF0000"> 
        <Setter Property="Foreground" Value="White" /> 
       </DataTrigger>      
      </Style.Triggers> 
     </Style> 
    </DataTemplate.Resources> 
</DataTemplate>` 

讓我知道如果我做錯了什麼。

+0

什麼是在這種情況下,「網格單元」? – mm8

+0

我正在使用Devexpress gridControl。 GridColumn的CellTemplate屬性綁定到這個數據模板。 – user3269603

回答

0

局部值,如您negativeToBrushConverter返回,優先於一種風格設置器所設置的值:https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/dependency-property-value-precedence

Style所以不管你的Foreground屬性設置爲,也不會

<TextBlock.Foreground> 
    <Binding Path="Value" Converter="{StaticResource negativeToBrushConvertor}" /> 
</TextBlock.Foreground> 

應設置在StyleForeground屬性的默認值:除非你刪除這部分應用。你應該設置{RelativeSource}到任何你的財產是的AncestorType

<DataTemplate> 
    <TextBlock HorizontalAlignment="Right" VerticalAlignment="Center"> 
     <TextBlock.Text> 
      <Binding Path="Value" StringFormat="###,###,###,###,###,###,##0.000;(###,###,###,###,###,###,##0.000)" /> 
     </TextBlock.Text> 
     <TextBlock.Style> 
      <Style TargetType="{x:Type TextBlock}"> 
       <Setter Property="Foreground"> 
        <Setter.Value> 
         <Binding Path="Value" Converter="{StaticResource negativeToBrushConvertor}" /> 
        </Setter.Value> 
       </Setter> 
       <Style.Triggers> 
        <DataTrigger Binding="{Binding Path=Background.Color,RelativeSource={RelativeSource AncestorType=dxg:GridCellContentPresenter}}" Value="#FF0000"> 
         <Setter Property="Foreground" Value="White" /> 
        </DataTrigger> 
       </Style.Triggers> 
      </Style> 
     </TextBlock.Style> 
    </TextBlock> 
</DataTemplate> 
+0

感謝您的及時回覆。但是,它仍然不起作用。我嘗試評論negativeToBrushConvertor,但仍然沒有觸發數據觸發器。按照你的建議,我使用了ancestorType。 – user3269603

相關問題