如何在 wpf 运行时更改字体颜色?
我正在使用 VS2010 - WPF - C#,我正在构建一个证券代码,在列表视图中向用户显示一些值。
我的列表视图看起来像这样:
<ListView Height="325" Margin="8,8,8,0" x:Name="listView1" VerticalAlignment="Top" BorderThickness="3" FontWeight="Bold" FontSize="12" Foreground="White" Background="{x:Null}" BorderBrush="{x:Null}" Style="{DynamicResource ListViewStyle1}">
<ListView.View>
<GridView>
<GridViewColumn Header="name" DisplayMemberBinding="{Binding company_name}" Width="200" />
<GridViewColumn Header="symbol" DisplayMemberBinding="{Binding symbol}" Width="50" />
<GridViewColumn Header="price" DisplayMemberBinding="{Binding price}" Width="75" />
<GridViewColumn Header="percent " DisplayMemberBinding="{Binding change_percent}" Width="50" />
</GridView>
</ListView.View>
</ListView>
我希望列表视图中的某些行颜色为红色,其他行颜色为绿色,具体取决于运行时的百分比值,但我不知道如何。
此致
I am using VS2010 - WPF - C# and I am building a securities ticker that show some values to the user in a listview.
My listview looks like this:
<ListView Height="325" Margin="8,8,8,0" x:Name="listView1" VerticalAlignment="Top" BorderThickness="3" FontWeight="Bold" FontSize="12" Foreground="White" Background="{x:Null}" BorderBrush="{x:Null}" Style="{DynamicResource ListViewStyle1}">
<ListView.View>
<GridView>
<GridViewColumn Header="name" DisplayMemberBinding="{Binding company_name}" Width="200" />
<GridViewColumn Header="symbol" DisplayMemberBinding="{Binding symbol}" Width="50" />
<GridViewColumn Header="price" DisplayMemberBinding="{Binding price}" Width="75" />
<GridViewColumn Header="percent " DisplayMemberBinding="{Binding change_percent}" Width="50" />
</GridView>
</ListView.View>
</ListView>
I would like some rows in my listview to be coloured in red, others in green, depending on the percent value at runtime, but I don't know how.
Best Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要执行您要查找的操作,HB的答案就是这样。
然而,从可用性的角度考虑这一点。有些人是色盲,会发现某些字体/背景颜色组合难以或无法阅读。您可能最好考虑在新的第一列中使用颜色椭圆,并根据用户在 Windows 中的选择保留标准背景/前景色。设置椭圆背景画笔的方法与 HB 对字体的答案相同。
即使对于非色盲的人来说,尝试阅读白色背景上的亮绿色文本也可能具有挑战性(例如,想象一下那些坐在后面有一扇窗户的人)。
只是一个想法。
To do what you're looking for, H.B's answer is it.
However consider this from a usability standpoint. Some people are colour-blind and will find certain font/background colour combinations difficult or impossible to read. You might be better considering having a colour ellipse in a new first column, and keeping the standard background/foreground colours as per the users' selections in Windows. The approach to setting the background brush of the ellipse would be the same as H.B's answer for the font.
Even for people who aren't colour-blind, trying to read bright green text on a white background might be challenging (imagine those people sat with a window behind them, for example).
Just a thought.
将控件的
Foreground
绑定到change_percent
并使用 ValueConverter 将其变成Brush
。这是一个从红色变为黄色到绿色的基本转换器:
您可以像这样使用它:
如何执行
xmlns
声明:您需要在某个名称空间中定义该类:
该名称空间可以是映射到
Window
或任何其他父控件中:这会将
MySolution.ValueConverters
命名空间映射到vc
前缀。有关更多参考信息请参阅 MSDN。Bind the
Foreground
of your control to thechange_percent
and use a ValueConverter to turn it into aBrush
.Here would be a basic converter which changes from red to yellow to green:
Which you could use like this:
How to do the
xmlns
declaration:You need to define the class in some namespace:
This namespace can be mapped in the
Window
or any other parent control:This maps the
MySolution.ValueConverters
namespace to thevc
prefix. For some more reference see MSDN.