WPF:DataGrid没有更新

时间:2017-06-28 08:28:58

标签: c# wpf xaml datagrid

问题:

我希望创建一个DataGrid,当新对象添加到ObservableCollection时会自动更新。作为原型,我创建了Timer,在ObservableCollection中添加了新的唯一对象。有问题的类继承自继承自ObservableObject接口的自定义INotifyPropertyChanged类。使用断点,我可以看到ObservableCollection更新,ObservableObject类被调用并设置正确的值,但DataGrid从不显示任何内容。

MainWindow.xaml:

<DataGrid Name="dg" ItemsSource="{Binding Path=DataColl, Mode=TwoWay}" >

</DataGrid>

MainWindow.xaml.cs:

public partial class MainWindow : Window
{

    public ObservableCollection<DataClass> DataColl = new ObservableCollection<DataClass>();
    public MainWindow()
    {
        InitializeComponent();

        this.DataContext = this;

        Timer aTimer;
        aTimer = new Timer();
        aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
        aTimer.Interval = 5000;
        aTimer.Enabled = true;

    }
    int index = 0;
            private void OnTimedEvent(object sender, ElapsedEventArgs e)
    {
        DataColl.Add(new DataClass() { ID = index, time = string.Format("{0:HH:mm:ss tt}", DateTime.Now),source="AIS"});
        index++;

    }


}

DataClass.cs

public class DataClass : ObservableObject
{
    private int _id;


    public int ID
    {
        get
        {
            return _id;
        }
        set
        {
            SetProperty(ref _id, value);
        }
    }
    public string time { get; set; }
    public string source { get; set;}
}

ObservableObject.cs

public class ObservableObject : INotifyPropertyChanged
{
        public event PropertyChangedEventHandler PropertyChanged;
 
        protected void OnPropertyChanged([CallerMemberName] string propName = null)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propName));
            }
        }
 
        protected bool SetProperty<T>(ref T storage, T value,
            [CallerMemberName] String propertyName = null)
        {
            if (Equals(storage, value)) return false;
            storage = value;
            OnPropertyChanged(propertyName);

            return true;
        }
}

感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

在我看来,这是非常糟糕的this.DataContext = this;请使用DependencyProperty

无论如何,我猜你使用了错误的Timer。请使用DispatcherTimer

  private DispatcherTimer _Timer = new DispatcherTimer();

  private void StartDispatcherTimer()
        {
            _Timer = new DispatcherTimer();
            _Timer.Interval = new TimeSpan(0, 0, 0, 0, 1000 / FPS);
            _Timer.Tick += new EventHandler(OnTimerTick);
            _Timer.Start();
        }

我想你明白了。

答案 1 :(得分:1)

将您的可观察集合声明更改为

private ObservableCollection<DataClass> dataColl; 
        public ObservableCollection<DataClass> DataColl
        {
            get { return dataColl; }
            set { dataColl = value; }
        }

初始化构造函数内的集合,并将计时器事件更改为

     private void OnTimedEvent(object sender, ElapsedEventArgs e)
            {
                Dispatcher.Invoke(() =>
                {
                    DataColl.Add(new DataClass() { ID = index, time = string.Format("{0:HH:mm:ss tt}", DateTime.Now), source = "AIS" });
                    index++;
                });   
           }