简体   繁体   English

OxyPlot触发LineSeries颜色更改通知

[英]OxyPlot trigger LineSeries color change notification

I have an oxyplot, and a listbox with ColorPickers to select the LineSeries colors. 我有一个oxyplot,以及一个带有ColorPickers的列表框,用于选择LineSeries颜色。

  • The ColorPickers are bound to the LineSeries through a value converter (I could not use the oxyplot default color converter, because the ColorPickers use nullable Color-s, so I had to "customize" the OxyPlot.Wpf.OxyColorConverter) ColorPickers通过值转换器绑定到LineSeries(我无法使用oxyplot默认颜色转换器,因为ColorPickers使用可为空的Color-s,因此我不得不“自定义” OxyPlot.Wpf.OxyColorConverter)
  • The color binding works in both directions: if I change the color in the ColorPickers, first the ConvertBack and then the Convert function is called. 颜色绑定在两个方向上都起作用:如果我在ColorPickers中更改颜色,则首先调用ConvertBack,然后调用Convert函数。 The LineSeries color and the ColorPicker colors are set. 设置LineSeries颜色和ColorPicker颜色。
  • At startup, I add the LineSeries to the PlotModel.Series (see below) 在启动时,我将LineSeries添加到PlotModel.Series(请参见下文)
  • After that, before the first DataPoints would be added, the ColorConverter.Convert function is called, with a value = {A:0, B:1, G:0, R:0}. 之后,在添加第一个DataPoints之前,将调用ColorConverter.Convert函数,其值为= {A:0,B:1,G:0,R:0}。 This sets the ColorPicker colors to some kind of transparent (the LineSeries colors do not change) 这会将ColorPicker颜色设置为某种透明(LineSeries颜色不变)

I guess, the problem is, that the LineSeries that are added to the PlotModel.Series do not have a valid color set, before I add DataPoints to them. 我想,问题是,在添加DataPoints之前,添加到PlotModel.Series的LineSeries没有有效的颜色集。

  • I did not find any RaisePropertyChanged or similar notifications on the Series or LineSeries instances. 在Series或LineSeries实例上找不到任何RaisePropertyChanged或类似的通知。
  • I tried to call RaisePropertyChanged("PlotModel"); 我试图调用RaisePropertyChanged(“ PlotModel”); after the first datapoint - does not help nor with any combination of "PlotModel.Series.Color" 在第一个数据点之后-对“ PlotModel.Series.Color”的任何组合都无济于事
  • The PlotModel.InvalidatePlot(true); PlotModel.InvalidatePlot(true); is called after each data point, but this does not notify the ColorPickers of color change. 在每个数据点之后调用,但这不会通知ColorPickers颜色变化。

So the question is: how can I make the ColorPickers take up the valid colors of the LineSeries after startup, before the ColorPickers are manually changed? 因此,问题是:在手动更改ColorPickers之前,如何使ColorPickers在启动后占用LineSeries的有效颜色?

I would like to avoid setting the colors manually at instantiation, right now I'm happy with the colors the PlotModel assigns to them. 我想避免在实例化时手动设置颜色,现在我对PlotModel分配给它们的颜色感到满意。

OxyPlot and ListBox: OxyPlot和ListBox:

<oxy:PlotView Grid.Column="0" Grid.Row="0" x:Name="plot1" Model="{Binding PlotModel}"/>
...
<ListBox Grid.Column="1" Grid.Row="0" ItemsSource="{Binding PlotModel.Series}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <CheckBox IsChecked="{Binding Path=IsVisible}" />
                <xctk:ColorPicker Width="30" ShowDropDownButton="False" SelectedColor="{Binding Path=Color, Mode=TwoWay,  UpdateSourceTrigger=PropertyChanged, Converter={StaticResource ColorConverter}}" Opacity="1" ShowRecentColors="True"/>
                <TextBlock Text="{Binding Path=Title}"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

The ColorConverter XAML resource: ColorConverter XAML资源:

<local:ColorConverter x:Key="ColorConverter"></local:ColorConverter>

And the C# code: 和C#代码:

[ValueConversion(typeof(OxyColor), typeof(Rect))]
class ColorConverter : IValueConverter
{      
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is OxyColor)
        {
            var color = (OxyColor)value;
            if ((targetType == typeof(Color)) || (targetType == typeof(Color?)))
            {
                return color.ToColor();
            }

            if (targetType == typeof(Brush))
            {
                return color.ToBrush();
            }
        }

        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (targetType == typeof(OxyColor))
        {
            if (value is Color)
            {
                var color = (Color)value;
                return OxyColor.FromArgb(color.A, color.R, color.G, color.B);
            }

            if (value is SolidColorBrush)
            {
                var brush = (SolidColorBrush)value;
                Color color = brush.Color;
                return OxyColor.FromArgb(color.A, color.R, color.G, color.B);
            }
        }

        return null;
    }

}

This is how I add new LineSeries dynamically: 这就是我动态添加新LineSeries的方式:

LineSeries l = new LineSeries { LineStyle = LineStyle.Solid, Title = title };
PlotModel.Series.Add(l);

Modifying the LineSeries instantiation works: 修改LineSeries实例的工作原理是:

LineSeries l = new LineSeries { LineStyle = LineStyle.Solid, Title = title, Color = PlotModel.DefaultColors[PlotModel.Series.Count] };

Is there another way? 还有另一种方法吗? Eg some kind of color property change event? 例如某种颜色属性更改事件?

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

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