Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

c# - How can I use a class or a ViewModel as the datasource for a ListView with DataTemplate that will show in Design View without compilation?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...