WPF将项目的计数绑定到TextBlock前景色

时间:2011-02-10 12:28:27

标签: c# wpf

我有一个转换器类,它将TagName的Count绑定到FontSize,如下所示。我现在想要实现的是FontSize的每3个增量绑定到不同的颜色。有人可以帮忙吗?

转换器

public class CountToFontSizeConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        const int minFontSize = 6;
        const int maxFontSize = 38;
        const int increment = 3;
        int count = (int)value;

        if ((minFontSize + count + increment) < maxFontSize)
        {
            return minFontSize + count + increment;            
        }
        return maxFontSize;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
    #endregion
}

XAML代码段

<DataTemplate x:Key="TagsTemplate">
        <WrapPanel>
            <TextBlock Text="{Binding Name, Mode=Default}" 
                       TextWrapping="Wrap" 
                       FontSize="{Binding ItemCount, Converter={StaticResource CountToFontSizeConverter}, Mode=Default}" 
                       Foreground="#FF0D0AF7" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        </WrapPanel>
    </DataTemplate>

新DataTemplate

<DataTemplate x:Key="TagsTemplate">
        <WrapPanel>
            <TextBlock Text="{Binding Name, Mode=Default}" 
                       TextWrapping="Wrap" 
                       FontSize="{Binding ItemCount, Converter={StaticResource CountToFontSizeConverter}, Mode=Default}" 
                       Foreground="{Binding count, Converter={StaticResource CountToBrushConverter}}"/>
        </WrapPanel>
    </DataTemplate>

2 个答案:

答案 0 :(得分:1)

低数量=黑色;
高计数=蓝色;

public class CountToBrushConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        const int cap = 32;
        int count = (int)value;
        count -= count % 3; // Modulo division to make sure
                            // that the value only changes
                            // every 3 steps

        double res = count <= cap ? count : cap; // Check if maximum
                                                 // has been reached
        res /= cap; // Normalize value to be between 0 and 1
        Color colour = new Color();
        colour.ScA = 1; // Set the alpha to full visibility
        colour.ScB = (float)res; // Set the blue channel to our value

        return new SolidColorBrush(colour);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }
    #endregion
}

答案 1 :(得分:0)

您可以创建另一个转换器,将当前字体大小转换为画笔。转换器可能看起来像这样:

public class FontSizeToBrushConverter : IValueConverter {
    public static readonly double Increment = 3;
    public static readonly double MinFontSize = 6;
    public static readonly double MaxFontSize = 32;

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
        if (value == null || value == DependencyProperty.UnsetValue) {
            return DependencyProperty.UnsetValue;
        }

        var fontSize = (double) value;

        double incrementsCount = MaxFontSize / Increment;

        var defaultColor = new SolidColorBrush(Colors.Black);

        for (int incrementIndex = 0; incrementIndex < incrementsCount; incrementIndex++) {
            if (fontSize == MinFontSize + Increment * incrementIndex) {
                switch (incrementIndex) {
                    case 0:
                        return new SolidColorBrush(Colors.Red);
                    case 1:
                        return new SolidColorBrush(Colors.Green);
                    case 2:
                        return new SolidColorBrush(Colors.Blue);
                    default:
                        return defaultColor; // Default color
                }
            }
        }

        return defaultColor; // Default color
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
        throw new NotImplementedException();
    }
}

您可能希望将此类顶部定义的常量移动到某个位置,以便在两个转换器之间共享它们。

使用此转换器,您的XAML将如下所示:

<DataTemplate x:Key="TagsTemplate">
    <WrapPanel>
        <TextBlock Text="{Binding Name, Mode=Default}" 
               TextWrapping="Wrap" 
               FontSize="{Binding ItemCount, Converter={StaticResource CountToFontSizeConverter}, Mode=Default}" 
               Foreground="{Binding FontSize, RelativeSource={RelativeSource Self}, Converter={StaticResource FontSizeToBrushConverter}}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </WrapPanel>
</DataTemplate>