【问题标题】:C# WPF listView not real time updating when addingC# WPF listView 添加时不实时更新
【发布时间】:2016-02-11 11:16:18
【问题描述】:

我有一个 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.

我是 Winforms 中 WPF 的新手,有一个 Mainform.Update。这里有类似的吗?

感谢任何帮助 帕特里克

【问题讨论】:

  • Dispatcher 不保证您的操作何时被调用。此外,您甚至没有指定DispatcherPriority。尝试将优先级设置为Send,看看是否有帮助。仅供参考,它永远不会完美,因为消息循环永远无法以接近“实时”的方式处理 UI 事件。
  • ocList 可能是一个可观察的集合,但你的代码没有显示任何绑定到它,你在哪里设置 ListView.ItemSource = {Binding ocList }?
  • BeginInvoke 是异步的。使用Invoke 进行同步调度程序调用。
  • @Clemens 请看我的编辑 2 我使用了调用,但这并没有改变。
  • @MikeT 对不起,我有它 lvInfo.ItemsSource = ocList;但请看我的编辑2

标签: c# wpf listview binding


【解决方案1】:

您需要将 ItemSource 属性绑定到实现 INotifyCollectionChanged 的集合

其中最简单的是ObservableCollection

如果您希望更新属性更改,PcdStatus 还应该实现 INotifyPropertyChanged

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-10
    • 1970-01-01
    • 2018-05-09
    • 1970-01-01
    • 2021-09-30
    • 2014-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多