簡體   English   中英

C#WPF listView添加時無法實時更新

[英]C# WPF listView not real time updating when adding

我有一個listView綁定到一個類:

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

而在Codebehind中,該類是

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; }
    }

該類通過事件更新:

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);           
    }

類更新正確但實時地完成。 我添加了一個console.Beep(),當從庫中觸發事件時會向我發出警告。 因此,庫中的事件---> My_PcdOnStatusChange ---> ocList.Add ---> listView已更新。 我希望每個蜂鳴聲都有更新,但是listView僅在所有s / r末尾更新。

編輯對不起,忘記了下面的ocList代表:

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

EDIT2我很確定問題不依賴於ListView或綁定本身。 我添加了一個更改圖片的屬性。 程序啟動時為紅色,空閑時為綠色。

  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))));
        }
    }

預期的行為是:

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

而其行為是:

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

我是WinPF中的WPF的新手,那里有一個Mainform.Update。 這里有類似的東西嗎?

感謝您的幫助Patrick

您需要將ItemSource屬性綁定到實現INotifyCollectionChanged的集合

其中最簡單的是ObservableCollection

如果要更新屬性更改,PcdStatus還應該實現INotifyPropertyChanged

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM