2014-11-21 77 views
-1

我在我的ControlTemplate中有Label,如果發生觸發器,我希望更改它的內容。我嘗試了很多不同的方式,但迄今爲止沒有運氣。這是我走了這麼遠,我可以改變它的apearance最接近但不Content使用觸發器設置標籤控件的內容

<Style x:Key="PartOptionsItemStyle" TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource InnerListViewItemsStyle}"> 
    <Style.Setters> 
    <Setter Property="Template"> 
     <Setter.Value> 
     <ControlTemplate TargetType="{x:Type ListViewItem}"> 
      <Border> 
      <Grid> 
       <Label x:Name="OptionPrice" HorizontalAlignment="Right" Content="{Binding Path=PriceDom}" ContentStringFormat="{}{0:C}" > 
       <Label.Resources> 
        <Style TargetType="{x:Type Label}"> 
        <Style.Triggers> 
         <DataTrigger Binding="{Binding Path=PriceDom}" Value="0"> 
         <Setter Property="Foreground" Value="Red"></Setter> 
         <Setter Property="Background" Value="Black"/> 
         <Setter Property="TextBlock.Text" Value="Free" /> 
         </DataTrigger> 
        </Style.Triggers> 
        </Style> 
       </Label.Resources> 
       </Label> 
      </Grid> 
      </Border> 
     </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    </Style.Setters> 
</Style> 

我開始試着寫在ControlTemplate.Triggers如下面這段代碼,但沒有連的外觀效果。

  <ControlTemplate.Triggers> 
      <Trigger SourceName="OptionPrice" Property="Content" Value="0"> 
       <Setter Property="Foreground" Value="Red" /> 
      </Trigger> 
      </ControlTemplate.Triggers> 

你會怎麼做,你會怎麼做?

+2

將默認的'Content'綁定爲setter到你的'Style'中。 '觸發器'不能覆蓋本地值。 – dkozl 2014-11-21 07:55:19

回答

3

Label沒有Text屬性,並且您已在標籤上直接設置Content。更新你的xaml,如下所示

 <Label x:Name="OptionPrice" HorizontalAlignment="Right" ContentStringFormat="{}{0:C}" > 
      <Label.Style> 
       <Style TargetType="{x:Type Label}"> 
       <Setter Property="Content" Value="{Binding Path=PriceDom}" /> 
       <Style.Triggers> 
        <DataTrigger Binding="{Binding Path=PriceDom}" Value="0"> 
        <Setter Property="Foreground" Value="Red"></Setter> 
        <Setter Property="Background" Value="Black"/> 
        <Setter Property="Content" Value="Free" /> 
        </DataTrigger> 
       </Style.Triggers> 
       </Style> 
      </Label.Style> 
      </Label> 
相關問題