OxyPlot触发LineSeries颜色更改通知

问题描述 投票:0回答:1

我有一个oxyplot,以及一个带ColorPickers的列表框来选择LineSeries颜色。

  • ColorPickers通过值转换器绑定到LineSeries(我无法使用oxyplot默认颜色转换器,因为ColorPickers使用可空的Color-s,所以我不得不“自定义”OxyPlot.Wpf.OxyColorConverter)
  • 颜色绑定在两个方向上都有效:如果我更改ColorPickers中的颜色,首先调用ConvertBack然后调用Convert函数。 LineSeries颜色和ColorPicker颜色已设置。
  • 在启动时,我将LineSeries添加到PlotModel.Series(见下文)
  • 之后,在添加第一个DataPoints之前,将调用ColorConverter.Convert函数,其值为{A:0,B:1,G:0,R:0}。这将ColorPicker颜色设置为某种透明(LineSeries颜色不会更改)

我想,问题是,在我向其添加DataPoints之前,添加到PlotModel.Series的LineSeries没有有效的颜色集。

  • 我没有在Series或LineSeries实例上找到任何RaisePropertyChanged或类似的通知。
  • 我试着调用RaisePropertyChanged(“PlotModel”);在第一个数据点之后 - 对“PlotModel.Series.Color”的任何组合都没有帮助
  • PlotModel.InvalidatePlot(true);在每个数据点之后调用,但这不会通知ColorPickers颜色更改。

所以问题是:在手动更改ColorPickers之前,如何让ColorPickers在启动后占用LineSeries的有效颜色?

我想避免在实例化时手动设置颜色,现在我对PlotModel分配给他们的颜色感到满意。

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>

ColorConverter XAML资源:

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

和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;
    }

}

这就是我动态添加新LineSeries的方法:

LineSeries l = new LineSeries { LineStyle = LineStyle.Solid, Title = title };
PlotModel.Series.Add(l);
c# wpf data-binding ivalueconverter oxyplot
1个回答
0
投票

修改LineSeries实例化的工作原理:

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

还有另外一种方法吗?例如。某种颜色属性变化事件?

© www.soinside.com 2019 - 2024. All rights reserved.