[英]ObservableCollection<T> not updating the UI
因此,我正在更新一個可觀察的集合,可以看到值已更改,但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。
我使用的圖表是Infragistics XamDataChart。 我懷疑問題是我實施課程的方式。 除了圖表之外,我還有更多需要更新的用戶界面,因此我希望一旦發現這一點,我就可以在這里進行操作。
這是我的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;
如果要在Station
對象上更改屬性,則需要在其上實現INotifyPropertyChanged
,並在屬性設置器中觸發PropertyChanged
事件。 當您的其他線程更改屬性時,似乎未通知Chart對象。 僅當添加或刪除Station對象時, ObservableCollection
才會導致圖表中的更新,而不是在其屬性更改時。
您說當您將鼠標懸停在圖表上時會看到更新的值,但這可能是因為圖表實際上是在屬性上調用getter來顯示該值,所以您將在那里看到更新的值(但我正在對此進行推測)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.