1

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

8
  • The Dispatcher doesn't guarantee when your action is invoked. Besides, you don't even specify a DispatcherPriority. Try setting the priority to Send and see if that helps. FYI, it will never be perfect as the message loop can never process UI events in anything near "real time". Commented Nov 10, 2015 at 14:40
  • ocList may be an observable collection but your code doesn't show any binding to it, where are you setting ListView.ItemSource = {Binding ocList }? Commented Nov 10, 2015 at 14:58
  • BeginInvoke is asynchronous. Use Invoke for synchronous dispatcher calls. Commented Nov 10, 2015 at 15:11
  • @Clemens please see my edit2 I use invoke but that doesnt' change. Commented Nov 10, 2015 at 15:17
  • @MikeT sorry my bad I have it lvInfo.ItemsSource = ocList; but please see my edit2 Commented Nov 10, 2015 at 15:17

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.