c# - OxyPlot 触发 LineSeries 颜色变化通知
问题描述
我有一个 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 颜色不会改变)
我想,问题是,添加到 PlotModel.Series 的 LineSeries 在我向它们添加 DataPoints 之前没有有效的颜色集。
- 我没有在 Series 或 LineSeries 实例上找到任何 RaisePropertyChanged 或类似通知。
- 我试图调用 RaisePropertyChanged("PlotModel"); 在第一个数据点之后 - 对“PlotModel.Series.Color”的任何组合都没有帮助
- PlotModel.InvalidatePlot(true); 在每个数据点之后调用,但这不会通知 ColorPickers 颜色变化。
所以问题是:在手动更改 ColorPickers 之前,如何让 ColorPickers 在启动后占用 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);
解决方案
修改 LineSeries 实例化工作:
LineSeries l = new LineSeries { LineStyle = LineStyle.Solid, Title = title, Color = PlotModel.DefaultColors[PlotModel.Series.Count] };
还有其他方法吗?例如某种颜色属性更改事件?
推荐阅读
- kubernetes - GPU 未添加到 GKE 节点池
- sql - Powershell - 复制名称匹配数组的文件夹
- angular - How to fix this error (error TS2322) in Angular
- r - 根据 x 轴在 plotly 上显示工具提示
- video-streaming - 关于减少 TBS2603 HDMI 视频编码器延迟的设置有什么建议吗?
- python - 即使使用非常好的初始起始参数,lmfit 也无法拟合或拟合没有不确定性,为什么?
- javascript - 下拉菜单的过渡效果不起作用
- ruby-on-rails - 如何在没有硬编码的情况下构建查询
- dataframe - 我无法在 julia 中打印 Dataframes 的列值或行值。我收到未找到的错误?
- css - 在 linode 与本地主机上使用 nginx、gunicorn、docker-compose 部署的 CSS 渲染不一致