使用 DataVisualization 图表库显示“幽灵”点 WPF MVVM C#

Ori*_*nlk 5 c# wpf xaml data-visualization mvvm

我有一个用 WPF MVVM 编写的程序,并且有一个使用 DataVisualization Chart 库(DVC.dll)在我的View上显示图形的视图

在我的ViewModel中,我有几个ObservableCollections<>,其中包含正好反映在图表上的数据点,但使用几分钟后,图表开始显示以前图表的“幽灵”点,并且直到我重新启动整个程序后才会刷新。

我尝试在每个集合更新新点之前使用 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>
Run Code Online (Sandbox Code Playgroud)

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; }
}
Run Code Online (Sandbox Code Playgroud)

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>();

         [....]
    }
Run Code Online (Sandbox Code Playgroud)