使用触发器设置标签控件的内容

编程入门 行业动态 更新时间:2024-10-26 20:31:07
本文介绍了使用触发器设置标签控件的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

更多推荐

使用触发器设置标签控件的内容

本文发布于:2023-11-12 05:01:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580577.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:触发器   控件   标签   内容

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!