使用itemsource更新datagrid

时间:2015-09-24 09:11:27

标签: c# wpf data-binding itemsource

我有一个Item源,它是ObservableCollection,但每当我更新它时。程序不显示项目源,直到我删除项目源并返回它。

当我这样做时它会卡住我的GUI。

 private ObservableCollection<DataForTable> _dataForTableRecieved; // a list to update the gui table.


    private void initVariables()
    {
          _dataForTableRecieved = new ObservableCollection<DataForTable>();   
           ThreadGui = new Thread(new ThreadStart(barak));
           ThreadGui.IsBackground = true;
           ThreadGui.Start();
     }


  private void b()
     {
        dataGridData.ItemsSource = "";
        dataGridData.ItemsSource = DataForTableRecieved;
        if (dataGridData.Columns.Count > 5)
        {
            dataGridData.Columns[5].Visibility = Visibility.Collapsed; // column data.
        }
     }

/// <summary>
    /// this method update the datagrid data table with the correct data.
    /// </summary>
    private void updateTablesOfGui()
    {
        DataForTable dt = new DataForTable();
        if (_QAllMessagesRecieved.Count > 0) // check recieve data 
        {


            _indexDataRecieved++;
            dt.LineNumber = (int)_indexDataRecieved;
            DateTime _Time = _QAllDateTimeMessgesRecieved.Dequeue();
            dt.Time = _Time.ToShortDateString() + "  " + _Time.ToLongTimeString() + "." + _Time.Millisecond.ToString();
            dt.RX_TX = "RX";
            byte[] dataArr = _QAllMessagesRecieved.Dequeue();
            dt.MsgType = _pd.ParseMeesage(dataArr, _toCheckCheckSum);
            dt.Data = ConvertFromBytesToString(dataArr);
            dt.ComPortName = "COM";

            this.Dispatcher.Invoke(updateTableGuiDelgate, dt);


        }
  }
    private void barak()
    {

        while (true)
        {
            this.Dispatcher.BeginInvoke(e);
            System.Threading.Thread.Sleep(100);
        }
    }



    /// <summary>
    /// this class contain a row for the data to show in the dataGridReceived
    /// </summary>
    public class DataForTable : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// this method used  for on property change to update the data in the table.
        /// </summary>
        /// <param name="propertyName"></param>
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new        PropertyChangedEventArgs(propertyName));
        }


        private int lineNumber; // the line number of row.            
        /// <summary>
        /// get or set contain the line number to show the data
        /// </summary>
        public int LineNumber
        {
            get { return lineNumber; }
            set
            {
                if (lineNumber != null)
                {
                    lineNumber = value;
                    OnPropertyChanged("LineNumber");
                }
            }
        }

        private string time; // contain the time of the data that has been sent or received. 

        /// <summary>
        /// get or sets the time that has been received.
        /// </summary>
        public string Time
        {
            get { return time; }
            set
            {
                if (time != null)
                {
                    time = value;
                    OnPropertyChanged("Time");
                }

            }
        }


        private string rX_TX; // if it is RX or TX


        /// <summary>
        /// this is the Com Port Name
        /// </summary>
        public string ComPortName
        {
            get { return comPortName; }
            set
            {
                if (comPortName != null)
                {
                    comPortName = value;
                    OnPropertyChanged("ComPortName");
                }
            }
        }

        /// <summary>
        /// get or set contain if this is a data that has been send from the RX or from the TX
        /// </summary>
        public string RX_TX
        {
            get { return rX_TX; }
            set
            {
                if (rX_TX != null)
                {
                    rX_TX = value;
                    OnPropertyChanged("RX_TX");
                }
            }
        }


        private string data; // contain the line of data the has been received.                        

        /// <summary>
        /// get or set the line of data that has been received or sent
        /// </summary>
        public string Data
        {
            get { return data; }
            set
            {
                if (data != null)
                {
                    data = value;
                    OnPropertyChanged("Data");

                }

            }
        }



        private string msgType;// the translation of the data per the XML.


        /// <summary>
        /// get or sets the translation from the xml into the dll
        /// </summary>
        public string MsgType
        {
            get { return msgType; }
            set
            {
                if (msgType != null)
                {
                    msgType = value;
                    OnPropertyChanged("MsgType");
                }

            }
        }

        private string comPortName;


        /// <summary>
        /// this is a constructor for updating the data init all the variables
        /// </summary>
        public DataForTable()
        {
            lineNumber = 0;
            time = "";
            rX_TX = "";
            data = "";
            msgType = "";
            comPortName = "";

        }

    }

1 个答案:

答案 0 :(得分:0)

在进行分配之前尝试这个:

_dataForTableRecieved.Clear();
_dataForTableRecieved = <the_updated_collection>;