I am trying to quickly style a DataGrid with DataTemplates and instead of relying on a compiled runtime DataSource, I want to quickly create a DataSource that can be used as the ItemsSource of my ListView to quickly edit the Styling.
C# Code
public class SampleViewModel
{
#region fields
private ObservableCollection<DATA> datas;
#endregion fields
#region properties
public ObservableCollection<DATA> DATAs { get { return this.datas; } }
#endregion properties
#region constructors
public SampleViewModel()
{
// Establish the invariant of owning a collection of BookSku.
this.datas = new ObservableCollection<DATA>();
if (DesignMode.DesignModeEnabled)
{
DataSource.LoadSampleDATAs(ref this.datas);
}
else
{
DataSource.LoadSampleDATAsfromCloud(ref this.datas);
}
}
#endregion constructors
}
public class DATA
{
#region properties
public DateTime Date { get; internal set; }
public string ProvNum { get; internal set; }
public decimal Fees { get; internal set; }
#endregion properties
}
internal static class DataSource
{
#region methods
public static void LoadSampleDATAs(ref ObservableCollection<DATA> datas)
{
datas.Add(new DATA()
{
Date = DateTime.Today.AddDays(-1),
ProvNum = "1",
Fees = (decimal)150
});
datas.Add(new DATA()
{
Date = DateTime.Today.AddDays(-2),
ProvNum = "2",
Fees = (decimal)250
});
}
public static void LoadSampleDATAsfromCloud(ref ObservableCollection<DATA> datas)
{
// In this simple app, we'll simulate real-world data access by loading sample data.
DataSource.LoadSampleDATAs(ref datas);
}
#endregion methods
}
XAML Code
<Page.DataContext>
<services:SampleViewModel/>
</Page.DataContext>
<syncfusion:SfDataGrid ItemsSource="{Binding DATAs}"/>
The Binding works at runtime but still does not work in design view and the Grid Shows up but doesn't show the Items. What am I doing wrong?
question from:
https://stackoverflow.com/questions/66067972/how-can-i-use-a-class-or-a-viewmodel-as-the-datasource-for-a-listview-with-datat 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…