我的Label
中有一个ControlTemplate
,如果发生触发,我希望改变它的内容。我尝试了很多不同的方法但到目前为止没有运气。这是我到目前为止最接近的,我可以改变它的外观而不是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>
你会做什么,你会怎么做?
答案 0 :(得分:4)
Label
没有Text
属性,您已直接在Label上设置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>