我有一个oxyplot和一个带有ColorPickers的列表框,用于选择LineSeries颜色。
我想,问题是,在添加DataPoints之前,添加到PlotModel.Series的LineSeries没有有效的颜色集。
所以问题是:在手动更改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);
答案 0 :(得分:0)
修改LineSeries实例的工作原理:
LineSeries l = new LineSeries { LineStyle = LineStyle.Solid, Title = title, Color = PlotModel.DefaultColors[PlotModel.Series.Count] };
还有另一种方法吗?例如。某种颜色属性更改事件?