[英]C# WPF listView not real time updating when adding
I have a listView binded to a class: 我有一个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>
while in Codebehind the class is 而在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; }
}
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.
我添加了一个console.Beep(),当从库中触发事件时会向我发出警告。 So Event from Library ---> My_PcdOnStatusChange ---> ocList.Add ---> listView updated.
因此,库中的事件---> My_PcdOnStatusChange ---> ocList.Add ---> listView已更新。 I expected an update per each beep but the listView is updated only at the end of all the s/r.
我希望每个蜂鸣声都有更新,但是listView仅在所有s / r末尾更新。
EDIT Sorry forgot to mention that the ocList below stands for: 编辑对不起,忘记了下面的ocList代表:
ObservableCollection<PcdStatus> ocList = new ObservableCollection<PcdStatus>();
EDIT2 I am pretty sure that the problem doesn't rely on the ListView or the binding itself. EDIT2我很确定问题不依赖于ListView或绑定本身。 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. 我是WinPF中的WPF的新手,那里有一个Mainform.Update。 Is there something similar here?
这里有类似的东西吗?
Thanx for any help Patrick 感谢您的帮助Patrick
you need to bind the ItemSource property to a collection that implements INotifyCollectionChanged 您需要将ItemSource属性绑定到实现INotifyCollectionChanged的集合
the simplest of these is ObservableCollection 其中最简单的是ObservableCollection
PcdStatus should also implement INotifyPropertyChanged if you want property changes to update 如果要更新属性更改,PcdStatus还应该实现INotifyPropertyChanged
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.