简体   繁体   English

绑定到静态属性时,ListView在WPF中未更新

[英]ListView is not updating in WPF when binding to static property

I have a Class ( SensorContainer ), that contains a static List ( SensorList ). 我有一个类( SensorContainer ),其中包含一个静态列表( SensorList )。

The class looks like this: 该类如下所示:

public static class SensorContainer
    {

        public static List<Sensor> SensorList
        {
            get { return _sensorList; }
        }

        private static readonly List<Sensor> _sensorList = new List<Sensor>();

    }

In my Xaml i have a listview that i bind to that SensorList with the following code 在我的Xaml中,我有一个列表视图,使用以下代码将其绑定到该SensorList

<ListView  ItemsSource="{Binding Source={x:Static local:SensorContainer.SensorList}}">

The binding works actually! 绑定实际上有效! But ...only when i do either change the window size or switch to another tab of the Gui and back !!! 但是 ...仅当我更改窗口大小切换到Gui的另一个选项卡后退 !!!

EDIT : To use an ObservableCollection would be possible as stated by pkeuter but not a possible solution at the moment due to specifications in other parts of the program. 编辑 :如pkeuter所述,使用ObservableCollection是可能的,但由于程序其他部分的规范,目前尚无法解决。

It has nothing to do with time or the comunication with the sensors...it just doesnt show the changes of the SensorList , as long as i dont play around with my Window :) ... 它与时间或与传感器的通信无关...只要我不玩我的Window,它就不会显示SensorList的变化:) ...

Anyone an idea about what the problem might be? 任何人都知道可能是什么问题吗?

Use an ObservableCollection . 使用一个ObservableCollection This will notify the UI of a change to your collection. 这将通知UI您的集合更改。 This way, you don't have to resize your window all the time ;-) 这样,您不必一直调整窗口大小;-)

Here is more information about the ObservableCollection: https://msdn.microsoft.com/en-us/library/ms668604(v=vs.110).aspx 这是有关ObservableCollection的更多信息: https ://msdn.microsoft.com/zh-cn/library/ms668604( v= vs.110).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM