我有一个binView绑定到类:
<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(),当从库中触发事件时,它会发出警告。来自图书馆的事件---&gt; My_PcdOnStatusChange ---&gt; ocList.Add ---&gt; 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.
我是Winform的WPF新手,有一个Mainform.Update。这里有类似的东西吗?
Thanx任何帮助 帕特里克
答案 0 :(得分:2)
您需要将ItemSource属性绑定到实现INotifyCollectionChanged
的集合最简单的是ObservableCollection
如果您想要更新属性更改
,PcdStatus也应该实现INotifyPropertyChanged