简体   繁体   中英

C# WPF listView not real time updating when adding

I have a listView binded to a class:

 <ListView x:Name="lvInfo" HorizontalAlignment="Left" Height="277" Margin="23,63,0,0" VerticalAlignment="Top" Width="750">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="#" Width="20" DisplayMemberBinding="{Binding execNumber}"/>
                <GridViewColumn Header="Function" Width="120" DisplayMemberBinding="{Binding currentFunction}"/>
                <GridViewColumn Header="Message" Width="300"  DisplayMemberBinding="{Binding pcdMessage}"/>
                <GridViewColumn Header="Event Type" Width="75" DisplayMemberBinding="{Binding pcdEventType}"/>
                <GridViewColumn Header="Event" Width="150" DisplayMemberBinding="{Binding pcdEvent}"/>
                <GridViewColumn Header="Timing" Width="50" DisplayMemberBinding="{Binding strTime}"/>
            </GridView>
        </ListView.View>
    </ListView>

while in Codebehind the class is

private class PcdStatus
    {
        public PcdStatus(int _execNumber, String _currentFunction, String _pcdMessage, Helper.ePcdEventType _pcdEventType, Helper.ePcdEvent _pcdEvent,String _strTime )
        {
            execNumber = _execNumber;
            currentFunction = _currentFunction;
            pcdMessage = _pcdMessage;
            pcdEventType = _pcdEventType;
            pcdEvent = _pcdEvent;
            strTime = _strTime;
        }
        public int execNumber { get; set; }
        public String currentFunction { get; set; }
        public String pcdMessage { get; set; }
        public Helper.ePcdEventType pcdEventType { get; set; }
        public Helper.ePcdEvent pcdEvent { get; set; }
        public String strTime { get; set; }
    }

the class is updated via event:

private void MyPcd_OnStatusChange(string _currentFunction, string _PCDMessage, Helper.ePcdEventType _pcdEventType, Helper.ePcdEvent _pcdEvent)
    {
        String strTime;
        if (lastTime == default(DateTime))       
            strTime = "---";
        else
        {
            TimeSpan ts = DateTime.Now - lastTime;
            strTime = ts.TotalSeconds.ToString("0.000")+ "\"";
        }
        lastTime = DateTime.Now;

        PcdStatus newStatus = new PcdStatus(execNumber, _currentFunction, _PCDMessage, _pcdEventType, _pcdEvent, strTime);
        Application.Current.Dispatcher.BeginInvoke(new Action(() => this.ocList.Add(newStatus)));
        //ocList.Add(newStatus);           
    }

the class update is done properly but not in real time. I have added a console.Beep() that warns me when the an event is fired from a library. So Event from Library ---> My_PcdOnStatusChange ---> ocList.Add ---> listView updated. I expected an update per each beep but the listView is updated only at the end of all the s/r.

EDIT Sorry forgot to mention that the ocList below stands for:

 ObservableCollection<PcdStatus> ocList = new ObservableCollection<PcdStatus>();

EDIT2 I am pretty sure that the problem doesn't rely on the ListView or the binding itself. I have added a property which changes a picture. Red when program started and green when idle.

  private bool _isBusy;
    public bool IsBusy
    {
        get { return _isBusy; }
        set
        {
            _isBusy = value;               
            if (value)
                imgBusy.Dispatcher.Invoke(new Action(() => imgBusy.Source = new BitmapImage(new Uri(@"../../Resources/redBall.png", UriKind.Relative))));
            else
                imgBusy.Dispatcher.Invoke(new Action(() => imgBusy.Source = new BitmapImage(new Uri(@"../../Resources/greenBall.png", UriKind.Relative))));
        }
    }

The expected behaviour is:

idle --> green
started ---> red
terminated ---> green

while its behaviour is:

idle ---> green
started --->green
midway ---> red
terminated --->green.

I am new with WPF in winforms there was a Mainform.Update. Is there something similar here?

Thanx for any help Patrick

you need to bind the ItemSource property to a collection that implements INotifyCollectionChanged

the simplest of these is ObservableCollection

PcdStatus should also implement INotifyPropertyChanged if you want property changes to update

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