繁体   English   中英

在绑定中遍历颜色

[英]Looping through colors in binding

我正在开发一个由其他人编写的大型代码项目,因此我试图添加一个影响尽可能小的更改。 它使用System.Windows.Controls.DataVisualization.Toolkit来创建一个折线MultiChart ,其中将多个LineSeries组合到一个MultiChart ,这是我的前任自己创建的一个类。

线条都是相同的颜色。 我被要求为它们设置不同的颜色。 或真的不止一种颜色。 行数不是恒定的,因此我希望遍历颜色列表。 然后,我要做的就是做一个足够大的工作以涵盖大多数情况。

由于行号不一致,因此无法明确定义每种颜色。 此外,该MultiChart具有SeriesSource属性,它是一个ObservableCollection多个的LineSeries其又一个ObservableCollection数据点,即通过结合视图模型设定。

因此,我认为我必须将颜色绑定到xaml之外的值。 由于它是与视图相关的工作,因此我认为后面的代码将是更改颜色的有效位置,但是我一直找不到能够在其中运行的解决方案。

我已经研究了关于SO和其他地方的许多问题,无论是否考虑MVVM模式,它们都没有奏效。 我只是在寻找可以将其重新用于MVVM的可行解决方案,但是如果这也是一种很好的做法,则可以加分。

我已经使用WPF已有一段时间了,由于某种原因,我无法完全围绕数据绑定工作,这可能就是为什么我无法找到大多数解决方案的原因。 尽管如此,我认为我已经接近:

<UserControl.Resources>

<local:LocalColorConverter x:Key="MyColorConverter"/>

<Style x:Key="dataPointStyle" TargetType="{x:Type charting:LineDataPoint}">
        <Setter Property="Background" Value="{Binding Path=DataContext.ColorCount, 
                                              RelativeSource={RelativeSource AncestorType=local:MultiChart, Mode=FindAncestor}, 
                                              Converter={StaticResource MyColorConverter}}"/>
</Style>

LocalColorConverter.cs

class LocalColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        int count = (int)value;
        //SolidColorBrush returnBrush = new SolidColorBrush();
        SolidColorBrush color = new SolidColorBrush();
        switch (count % 5)
        {
            case 0:
                color.Color = Colors.Blue;
                break;
            case 1:
                color.Color = Colors.Green;
                break;
            case 2:
                color.Color = Colors.Red;
                break;
            case 3:
                color.Color = Colors.Purple;
                break;
            case 4:
                color.Color = Colors.Yellow;
                break;
        }
        return color;
    }

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

我意识到这在实践中不是很好。 它依赖于视图模型中的ColorCount值,该值是与视图相关的信息。 MVVM不好,但是我只是在使其适应模式之前试图使其正常工作。

另外,由于ColorCount从未更改,因此它实际上不能按ColorCount工作。 最初在viewmodel中设置为0。 如果我继续沿着这条路走,我想增加ColorCount的值,以便每次使用Converter时颜色都会改变。

我尝试在Converter返回之前只做((int)value)++ ,但这没有用。 我没想到会这样,但是它使您对我正在寻找的东西有所了解。

否则,我认为我最好的解决方案是在后面的代码中执行此操作。 但是我不知道在哪里/怎么做。 UserControl进入新的LineSeries时,将不得不更改颜色。 后面的当前代码实现了LineSeries_Loaded事件处理程序,但是未在行中设置颜色,而是为行中的每个点设置了颜色。 因此,我需要在每行增加我的颜色计数器,然后在每个点上使用计数来确定它的颜色。

我已经尝试过了,但是我似乎无法以这种方式找到Background属性。 请注意,图表是为MultiChart对象提供的名称。

private void LineSeries_Loaded(object sender, RoutedEventArgs e)
    {
        foreach(LineSeries line in chart.Series.Cast<LineSeries>())
        {
            foreach(LineDataPoint point in line)
            {

            }
        }
    }

但是我得到的错误是该行没有公共GetEnumerator。

有什么建议么?

好吧,看来写这个问题让我的创意源源不断。 我记得我曾尝试在后面的代码中设置样式设置器,但是您无法在加载时修改样式。 但是我只是意识到我可以随时切换样式。 所以我做了这些:

<Style x:Key="blueDataPointStyle" TargetType="{x:Type charting:LineDataPoint}" BasedOn="{StaticResource dataPointStyle}">
    <Setter Property="Background" Value="Blue"/>
</Style>

<Style x:Key="redDataPointStyle" TargetType="{x:Type charting:LineDataPoint}" BasedOn="{StaticResource dataPointStyle}">
    <Setter Property="Background" Value="Red"/>
</Style>

<Style x:Key="greenDataPointStyle" TargetType="{x:Type charting:LineDataPoint}" BasedOn="{StaticResource dataPointStyle}">
    <Setter Property="Background" Value="Green"/>
</Style>

然后,在后面的代码中:

private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        int count = 0;
        foreach (LineSeries line in chart.Series)
        {
            switch (count % 3)
            {
                case 0:
                    line.DataPointStyle = Resources["blueDataPointStyle"] as Style;
                    break;
                case 1:
                    line.DataPointStyle = Resources["redDataPointStyle"] as Style;
                    break;
                case 2:
                    line.DataPointStyle = Resources["greenDataPointStyle"] as Style;
                    break;
            }
            count++;

        }
    }

工作正常! 好吧,大多数情况下,其他所有行都被分组为一个行,因此我仍有一些工作要做。 但这就是我所问的答案。 另外,它似乎适合MVVM。 事实证明,我不需要绑定(可能是为什么我能提出它)。 但是我仍然对其他想法感兴趣。 我想要一个适当的绑定示例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM