简体   繁体   中英

ListView is not updating in WPF when binding to static property

I have a Class ( SensorContainer ), that contains a static List ( 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

<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 !!!

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.

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 :) ...

Anyone an idea about what the problem might be?

Use an ObservableCollection . This will notify the UI of a change to your collection. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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