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

发布于 2024-11-03 19:23:56 字数 938 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小苏打饼 2024-11-10 19:23:56

要执行您要查找的操作,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.

神仙妹妹 2024-11-10 19:23:56

将控件的 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

Bind the Foreground of your control to the change_percent and use a ValueConverter to turn it into a Brush.

Here would be a basic converter which changes from red to yellow to green:

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();
    }
}

Which you could use like this:

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

How to do the xmlns declaration:

You need to define the class in some namespace:

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

This namespace can be mapped in the Window or any other parent control:

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

This maps the MySolution.ValueConverters namespace to the vc prefix. For some more reference see MSDN.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文