[C#] [UWP]设置TextBlock根据文本运行前景色 [英] [C#][UWP] Set TextBlock Run foreground color based on text

查看:347
本文介绍了[C#] [UWP]设置TextBlock根据文本运行前景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据文字为文本块中的"运行"项目的前景着色。 例如,如果"DisplayName",在下面的代码示例中是"管理员"然后我希望前景是红色的,否则是白色的。 有没有
a的方法呢?



 

< TextBlock Grid.Row =" 1" TextWrapping = QUOT;包覆与QUOT;前景= QUOT;白色" FontFamily =" Myriad Pro"字号= QUOT 18 QUOT;
fontWeight设置= QUOT; SemiBold" Grid.Column = QUOT 1 QUOT; VerticalAlignment = QUOT;中心"的Horizo​​ntalAlignment = QUOT;拉伸">
< Run Text =" {x:Bind LocalDateTime,Converter = {StaticResource StringFormatConverter},
ConverterParameter ='{} {0:[HH \\\\\:mm tt] }"}" />
< Run Text =" {x:Bind DisplayName}" />
< Run Text =":" />
< Run Text =" {x:Bind ChatMessage}" />
< / TextBlock>

解决方案

您好,


您可以尝试设置
运行
项目将改变文本部分的颜色。因此,您也可以使用转换器将color属性绑定到文本。检测文本以查看它是否是您想要的文本并输出正确的颜色。 


祝您好运,


Roy


I would like to color the foreground of a Run item in a textblock based on the text.  For instance if "DisplayName" in the code example below is "Admin" then I would like the foreground to be red, otherwise white.  Is there a way to do this?

<TextBlock Grid.Row="1" TextWrapping="Wrap" Foreground="White" FontFamily="Myriad Pro" FontSize="18"
FontWeight="SemiBold" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Stretch"> <Run Text="{x:Bind LocalDateTime, Converter={StaticResource StringFormatConverter},
ConverterParameter='{}{0:[HH\\\\:mm tt]}'}" /> <Run Text="{x:Bind DisplayName}" /> <Run Text=": " /> <Run Text="{x:Bind ChatMessage}" /> </TextBlock>

解决方案

Hi,

You could try to set the Foreground property of the Run Item which will change the color the part of text. So you could bind the color property to the text using a converter as well. Detect the text to see if it is the text you want and output the right color. 

Best regards,

Roy


这篇关于[C#] [UWP]设置TextBlock根据文本运行前景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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