【问题标题】:OxyPlot trigger LineSeries color change notificationOxyPlot 触发 LineSeries 颜色变化通知
【发布时间】:2019-02-01 02:33:39
【问题描述】:

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

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

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

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

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

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

OxyPlot 和列表框:

<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】:

    修改 LineSeries 实例化工作:

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

    还有其他方法吗?例如。某种颜色属性更改事件?

    【讨论】:

    • 这个答案解决了一个问题,即所选线在图表中的颜色不准确,手动创建了可检查的过滤器,但我需要它来处理一堆线(又名系列)。最新的 OxyPlot 版本有 11 种默认颜色,因此请使用 C# 模运算符。所以建议: int index = (PlotModel.Series.Count == 0) ? 0 : ((PlotModel.Series.Count - 1) % PlotModel.DefaultColors.Count); LineSeries ls = new LineSeries() { Title = sq, Color = PlotModel.DefaultColors[index] };
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    • 2013-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多