I have a simple DataGrid which I am binding to a ObservableCollection and its producing black small lines in the Grid with no Data Visible. I am using ObservableCollection as I have the collection being built in RunTime using Reflection.
I am doing something like this
XAML
<DataGrid ItemsSource="{Binding Data}" />
C#
public ObservableCollection<object> Data
{
get { return _Data; }
set {
this._deals = value;
this.NotifyPropertyChanged("Deals");
}
}
public Run()
{
this.Data = CreateData(typeof(MyRecordClass)) //'MyRecordClass' needs to be passed at runtime
}
public ObservableCollection<Object> CreateData(Type RecordType)
{
ObservableCollection<Object> data = new ObservableCollection<object>();
var record = Activator.CreateInstance(RecordType);
// Logic to load the record with Data
data.Add(record);
return data;
}
Is there a way in which I can make the DataGrid read an ObservableCollection without specifying the ColumnNames or create a ObservableCollection object in the CreateData function ?
Your collection should have public properties, so datagrid could bind columns to it.
If you use collection type of Object than you have no properies for binding, so the empty rows will be displayed.
Here is example for you:
public partial class MainWindow : Window
{
public ObservableCollection dataSource;