我有一个在WPF MVVM中编写的程序,我有一个 View ,它使用DataVisualization Chart库(DVC.dll)在我的 View 上显示图形。
在我的 ViewModel 中,我有几个 ObservableCollections<> ,其中包含恰好反映在图表上的DataPoints,但在使用几分钟后,图表开始显示& #34;鬼"之前图表的分数并且在我重新启动整个程序之前不会刷新。
我尝试在使用新点更新每个Collection之前使用Clear()函数,并在更改集合中的数据时添加了一个小的Thread.Sleep()延迟,但没有任何帮助。
任何解决方案?
查看:
<DVC:Chart Title="Cross Section" Background="LightSteelBlue" Height="385" >
<DVC:Chart.Series>
<!--Ground Line-->
<DVC:LineSeries IsSelectionEnabled="True" IndependentValueBinding="{Binding Path=X}"
SelectedItem="{Binding SelectedPointOnGraph, Mode=TwoWay}"
DependentValueBinding="{Binding Path=Y}" ItemsSource="{Binding HydroPoints, BindsDirectlyToSource=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
TransitionDuration="0">
<DVC:LineSeries.DataPointStyle>
<Style TargetType="{x:Type DVC:LineDataPoint}">
<Setter Property="Background" Value="SaddleBrown" ></Setter>
<Setter Property="Opacity" Value="2" />
<Setter Property="ToolTip" Value="{Binding Path=Coordinate}" />
<Setter Property="Template" >
<Setter.Value>
<ControlTemplate TargetType="DVC:LineDataPoint">
<Grid>
<Ellipse Fill="{TemplateBinding Background}" />
<Canvas>
<TextBlock FontWeight="Bold"
Text="{Binding Path=PointNote, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>
</Canvas>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DVC:LineSeries.DataPointStyle>
<DVC:LineSeries.Template>
<ControlTemplate TargetType="DVC:LineSeries">
<Canvas x:Name="PlotArea">
<Polyline x:Name="polylinee"
Points="{TemplateBinding Points}"
Stroke="SaddleBrown"
Style="{TemplateBinding PolylineStyle}" />
</Canvas>
</ControlTemplate>
</DVC:LineSeries.Template>
</DVC:LineSeries>
</DVC:Chart.Series>
</DVC:Chart>
HydroPoints.cs(模型文件与使用OnPropertyChanged事件的set \ get具有相同的属性)
public class HydroPoint : BusinessObjectBase
{
public int PointID { get; set; }
public int StationID { get; set; }
public int SectionID { get; set; }
public DateTime SectionMesurDate { get; set; }
public Single X { get; set; }
public Single Y { get; set; }
public string PointNote { get; set; }
public Single PointRoughnessCoefficient { get; set; }
}
HydroMainViewModel.cs
public ObservableCollection<HydroPoint> HydroPoints
{
get { return _hydroPoints.OrderBy(x => x.X).ToObservableCollection(); }
set
{
if (_hydroPoints == value)
return;
_hydroPoints= value;
OnPropertyChanged("HydroPoints");
}
}
public void SetSelectedSectionByConboBox()
{
if (RPointsList != null && RPointsList.Count >= 0)
RPointsList.Clear();
if (HydroPoints != null && HydroPoints.Count >= 0)
HydroPoints.Clear();
[....]
HydroPoints = Repository.HydroPointsRepository.HydroPoints.Where(X => X.StationID == CurrentStation.SerialCode && X.SectionID == SelectedSectionID && X.SectionMesurDate == _selectedMesurDate).ToObservableCollection<HydroPoint>();
RPointsList = Repository.HydroPointsRepository.RPoints().Where(X => X.StationID == CurrentStation.SerialCode && X.SectionID == SelectedSectionID && X.SectionMesurDate == SelectedSectionMesurDate).ToObservableCollection<HydroPoint>();
[....]
}