使用触发器设置标签控件的内容 [英] Setting a Content of Label control using Triggers

查看:77
本文介绍了使用触发器设置标签控件的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 ControlTemplate 中有一个 Label ,如果发生触发器,我希望更改其内容。我尝试了许多不同的方法,但到目前为止还没有运气。这是到目前为止我可以更改的最接近的外观,但是可以更改其外观,但不能更改内容

I have got a Label in my ControlTemplate that I wish to change it's content if a trigger happens. I have tried so many different ways but no luck so far. This is the closest I come so far which I can change its apearance but not 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

I initially tried to write this code in the ControlTemplate.Triggers such as below but that didn't even effect the appearance.

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

您会做什么,怎么做?

推荐答案

标签没有 Text 属性,您已直接在Label上设置了 Content 。如下更新您的xaml

Label does not have Text property and you have set Content directly on Label. Update your xaml as below

       <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>

这篇关于使用触发器设置标签控件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆