【问题标题】:how can I change font color during runtime in wpf?如何在 wpf 运行时更改字体颜色?
【发布时间】:2011-08-14 12:47:29
【问题描述】:

我正在使用 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>

我希望我的列表视图中的某些行用红色着色,其他行用绿色着色,具体取决于运行时的百分比值,但我不知道如何。

最好的问候

【问题讨论】:

  • 如何设置行的颜色?字体将是类似的方法...
  • 我根本没有设置行的颜色,我在字体颜色中提到的是文本的颜色
  • 啊抱歉,我没有正确阅读您的问题。我以为您是在说您已经在更改行背景,并且需要更改字体以使其保持可读性。 :)

标签: c# wpf xaml colors


【解决方案1】:

要做你想做的事,H.B's answer 就是它。

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

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

只是一个想法。

【讨论】:

    【解决方案2】:

    将控件的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 前缀。更多参考see MSDN

    【讨论】:

    • 我知道我要求的太多但我不知道该怎么做?
    • 我会尝试扩展我的答案,不过可能需要一段时间。
    • 其实没有,不过我可以google一下,我想应该没那么难
    • 这就是为什么我问你是否知道如何处理 xml 命名空间。您需要在某个父控件中定义一个xmlns,它指向包含PercentToBrushConverter 类的命名空间。例如xmlns:vc="clr-namespace:MyApplication.Converters"
    猜你喜欢
    • 1970-01-01
    • 2010-10-28
    • 1970-01-01
    • 2013-06-15
    • 2010-10-21
    • 1970-01-01
    • 1970-01-01
    • 2018-04-07
    • 1970-01-01
    相关资源
    最近更新 更多