【问题标题】:ObservableCollection<T> not updating the UIObservableCollection<T> 不更新 UI
【发布时间】:2013-06-30 17:06:43
【问题描述】:

所以我有一个正在更新的可观察集合,我可以看到值已经改变,但 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。我怀疑问题出在我实施课程的方式上。我有更多的 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;

【问题讨论】:

  • 如果您尝试绑定的是locations 集合,那么不应该是ItemsSource="{Binding locations}"吗?
  • 哦,抱歉后面的代码我有 XAxis.ItemsSource = GeoLocations.locations; Series1.ItemsSource = GeoLocations.locations;

标签: c# data-binding binding observablecollection infragistics


【解决方案1】:

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

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

【讨论】:

  • 哦。我会试试这个。你说的很有道理。作为另一种测试方法,听起来好像我删除了位置的所有元素,然后添加了新元素,这些元素会在可观察集合中触发事件,从而重新生成图表。
  • 其实我没有意识到实现InotifyPropertyChanged只是几行代码。所以我就这么做了,维奥拉!我的随机代码生成得太快,所以我的图表看起来有点疯狂,但很容易修复。我感觉很好。
猜你喜欢
  • 2014-12-15
  • 2017-12-28
  • 2019-03-16
  • 2016-11-05
  • 1970-01-01
  • 2012-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多