WPF 标签样式触发器基于标签内容中未知索引处的字符,是否可能?
当表单上的字段要求发生变化时,是否可以使用触发器检查 Label.Contents 中的最后一个字符是否为“*”,如果是,则在标签上设置属性,而不是以编程方式设置样式?
类似这样,但如何检查 Content 属性的最后一个字符?
<Style x:Key="LabelStandard" TargetType="Label">
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Style.Triggers>
<Trigger Property="Content" Value="*"> <!-TODO only check the last char -->
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
Rather than programatically set the the style when the requirement of a field on a form changes is it possible to use a trigger that checks the last char in a Label.Contents is a '*' and if so set a property on the Label?
Something like this but how to check on the last char of the Content property?
<Style x:Key="LabelStandard" TargetType="Label">
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Style.Triggers>
<Trigger Property="Content" Value="*"> <!-TODO only check the last char -->
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你必须为此使用转换器。尝试使用类似
Update
的内容,只要您知道其位置,您就可以绑定到
Content
字符串中的任何给定字符。但是,如果您的字符串长度会有所不同(我认为是这种情况),那对您没有多大好处,因为您无法在绑定内绑定
[2]
(以我认为的任何方式)我知道)。除此之外,我认为您必须按照您自己指出的那样执行代码隐藏解决方案
I think you'll have to use a Converter for that. Try with something like this
Update
You can bind to any given character in the
Content
string as long as you know its position.But if your string length will vary (which I assume is the case) that won't do you much good since you can't bind the
[2]
inside the binding (in any way that I'm aware of).Other than this, I think you'll have to do a code behind solution as you pointed out yourself