ObservableCollection<T> not updating the UI

本文关键字:the updating UI not lt ObservableCollection gt | 更新日期: 2023-09-27 18:20:40

所以我有一个正在更新的可观察集合,我可以看到值已经更改,但UI没有更改。这是我的

public static class GeoLocations
{
    public static ObservableCollection<Station> _locations = new ObservableCollection<Station>();
    public static ObservableCollection<Station> locations
    {
        get { return _locations; }
        set 
        { 
            _locations = value;      
        }
    }
}
public class Station
{
    public int StationNumber { get; set; }
    // Bunch more properties here ...
}

然后我有一个类,它为另一个线程中的所有属性生成随机数。我知道它会改变它们,因为当我将鼠标悬停在我使用的图表中的项目上时,我会看到它在更新。然而,图表本身并没有更新。假设第一个数字是1.6。然后更新到1.4 0.5 1.2…图表保持在1.6。

我使用的图表是Infrastics XamDataChart。我怀疑问题出在我实现类的方式上。我有更多的UI需要更新,而不仅仅是图表,所以我希望一旦我弄清楚了这一点,我就能在这里实现这些功能。

这是我的XamDataChart 的xaml

<ig:XamDataChart Style="{StaticResource BarChartStyle}" Grid.Row="1" Grid.Column="1">
        <ig:XamDataChart.Axes>
            <ig:NumericYAxis x:Name="YAxis" MinimumValue="0" Interval="0.4" Label="{}{}" FontSize="8" MaximumValue="1.7" MajorStroke="#FF2C2C2C" Foreground="White" BorderBrush="White" FontWeight="Bold">
                <ig:NumericYAxis.LabelSettings>
                    <ig:AxisLabelSettings Extent="23" Foreground="White"/>
                </ig:NumericYAxis.LabelSettings>
            </ig:NumericYAxis>
            <ig:CategoryXAxis x:Name="XAxis" 
                              ItemsSource="{Binding}" 
                              Label="{}{IndexNum}" 
                              Interval="1" 
                              MajorStroke="Black" 
                              Foreground="White" 
                              BorderBrush="White"
                              DataContextChanged="CollectionChanged">
                <ig:CategoryXAxis.LabelSettings>
                    <ig:AxisLabelSettings Extent="17" VerticalAlignment="Bottom" FontSize="11" Foreground="White"/>
                </ig:CategoryXAxis.LabelSettings>
            </ig:CategoryXAxis>
        </ig:XamDataChart.Axes>
        <ig:XamDataChart.Series>
            <ig:ColumnSeries Style="{StaticResource ColumnSeriesStyle}" x:Name="Series1" ValueMemberPath="StationNumber "  XAxis="{Binding ElementName=XAxis}"  YAxis="{Binding ElementName=YAxis}" Brush="DodgerBlue" Outline="Black">
            </ig:ColumnSeries>
        </ig:XamDataChart.Series>
    </ig:XamDataChart>

背后的代码

XAxis.ItemsSource = GeoLocations.locations;
Series1.ItemsSource = GeoLocations.locations;

ObservableCollection<T> not updating the UI

如果Station对象的属性正在更改,则需要在其上实现INotifyPropertyChanged,并在属性设置器中激发PropertyChanged事件。当您的其他线程更改属性时,似乎没有通知图表对象。ObservableCollection只会在添加或删除Station对象时在图表中引起更新,而不会在其属性更改时引起更新。

你说当你把鼠标悬停在图表上时,你会看到更新的值,但这可能是因为图表实际上正在调用属性上的getter来显示它,所以你会在那里看到更新后的值(但我只是猜测)。