我在网上搜索了很多这个问题,但我没有解决这个问题。所以要解决这个问题,我按照以下步骤操作。
1.)首先我通过以下方式使绘图仪图例可见性为假
plotter.LegendVisible = false;
2.)其次,我在出现绘图仪图例的图形上添加了一个列表视图控件。
<ListView Height="Auto" HorizontalAlignment="Center" Margin="1100,139,0,0" Name="listview" ItemsSource="{Binding Items}" HorizontalContentAlignment="Stretch" VerticalAlignment="Top" Width="75" Grid.RowSpan="2" MaxHeight="260">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Text}" Foreground="{Binding BackgroundColor}">
</TextBlock>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.BorderBrush>
<SolidColorBrush />
</ListView.BorderBrush>
</ListView>
3.) 然后我在后端做一些工作 -
-添加 ItemVM.cs :
class ItemVM : INotifyPropertyChanged
{
private string TextValue = String.Empty;
private Brush BackgroundColorValue = null;
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
public ItemVM(Brush color, string objectData)
{
BackgroundColor = color;
Text = objectData;
}
public string Text
{
get
{
return this.TextValue;
}
set
{
if (value != this.TextValue)
{
this.TextValue = value;
NotifyPropertyChanged("Text");
}
}
}
public Brush BackgroundColor
{
get
{
return this.BackgroundColorValue;
}
set
{
if (value != this.BackgroundColorValue)
{
this.BackgroundColorValue = value;
NotifyPropertyChanged("BackgroundColor");
}
}
}
}
-在 mainform.xaml.cs 中:
List<ItemVM> Items;
List<string> lst = new List<string> {"item1","item2","item3" };
var converter = new System.Windows.Media.BrushConverter();
Color[] colors = ColorHelper.CreateRandomColors(3);
Items = new List<ItemVM>();
for(int i=0;i<lst.count;i++)
{
Items.Add(new ItemVM((Brush)converter.ConvertFromString(colors[i].ToString()), SelectedItems[i]));
}
plotter.LegendVisible = false;
listview.ItemsSource = Items;
现在我在图表绘图仪上获得了带有滚动和重新生成前景色的图例框也反映了图表线条颜色。