如何在wpf运行时更改字体颜色

本文关键字:字体 颜色 运行时 wpf | 更新日期: 2023-09-27 17:54:03

我正在使用VS2010 - WPF - c#,我正在构建一个证券报价器,在列表视图中向用户显示一些值。

我的listview是这样的:

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

我想在我的列表视图中的一些行颜色为红色,在绿色的其他,取决于运行时的百分比值,但我不知道如何。

如何在wpf运行时更改字体颜色

要做你想做的事,h.b.的答案就是它。

但是,从可用性的角度考虑这一点。有些人是色盲,会发现某些字体/背景颜色组合很难或根本无法阅读。你可能会更好地考虑在新的第一列中有一个颜色椭圆,并根据用户在Windows中的选择保持标准的背景/前景颜色。设置椭圆背景笔刷的方法与h.b.对字体的回答相同。

即使对于没有色盲的人来说,试图在白色背景上阅读亮绿色的文本也可能是一项挑战(例如,想象一下那些坐在他们身后有一扇窗户的人)。

只是一个想法。

将控件的Foreground绑定到change_percent,并使用ValueConverter将其转换为Brush

这将是一个基本的转换器,从红色到黄色再到绿色:

public class PercentToBrushConverter : IValueConverter
{
    //http://stackoverflow.com/questions/3722307/is-there-an-easy-way-to-blend-two-system-drawing-color-values/3722337#3722337
    private Color Blend(Color color, Color backColor, double amount)
    {
        byte r = (byte)((color.R * amount) + backColor.R * (1 - amount));
        byte g = (byte)((color.G * amount) + backColor.G * (1 - amount));
        byte b = (byte)((color.B * amount) + backColor.B * (1 - amount));
        return Color.FromRgb(r, g, b);
    }
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        //Assumes the percent property to be an int.
        int input = (int)value;
        Color red = Colors.Red;
        Color yellow = Colors.Yellow;
        Color green = Colors.Green;
        Color color;
        if (input <= 50)
        {
            color = Blend(yellow, red, (double)input/50);
        }
        else
        {
            color = Blend(green, yellow, (double)(input - 50) / 50);
        }
        return new SolidColorBrush(color);
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

你可以这样使用:

<ListView>
    <ListView.Resources>
        <vc:PercentToBrushConverter x:Key="PercentToBrushConverter"/>
    </ListView.Resources>
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Progress">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <!-- An indicator ellipse as suggested by Neil Barnwell -->
                            <Ellipse Height="16" Width="16" Fill="{Binding change_percent, Converter={StaticResource PercentToBrushConverter}}"/>
                            <TextBlock Margin="5,0,0,0" Text="{Binding change_percent}"/>
                        </StackPanel>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <!-- ... -->
        </GridView>
    </ListView.View>
</ListView>

如何做xmlns声明:

您需要在某个命名空间中定义类:

namespace MySolution.ValueConverters
{
    public class PercentToBrushConverter : IValueConverter { /*...*/ }
}

这个命名空间可以映射到Window或任何其他父控件中:

<Window ...
    xmlns:vc="clr-namespace:MySolution.ValueConverters">

MySolution.ValueConverters命名空间映射到vc前缀。有关更多参考资料,请参阅MSDN.