如何在VS WPF设计面板中显示收集数据?

PiZ*_*zL3 6 c# data-binding wpf visual-studio-2010

如何让VS ListView在"设计"面板中填充示例数据?

我很难过.我做了很多阅读和谷歌搜索,无法找到一个可靠的答案.在VS Design面板中显示虚假数据将大大加快主题,因为我不必每隔几次更改就调试它.

这是我的代码:

XAML:

<ListView DataContext="{Binding}"
          Name="PeopleListView">
    <ListView.ItemTemplate>
        <DataTemplate DataType="Person">
                <WrapPanel>
                    <TextBlock Text="{Binding Path=FirstNameView}"/>
                    <TextBlock Text="{Binding Path=LastNameView}" />
                </WrapPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
Run Code Online (Sandbox Code Playgroud)

采集:

public class People : ObservableCollection<Person>
{
    public People()
    {
        this.Add(new Person() { FirstName = "John", LastName = "Doe" });
        this.Add(new Person() { FirstName = "John", LastName = "Doe" });
        this.Add(new Person() { FirstName = "John", LastName = "Doe" });
    }
}
Run Code Online (Sandbox Code Playgroud)

主要:

private People PList;

public MainWindow()
{
    this.PList = new People();
    PeopleListView.ItemsSource = this.PList;
}
Run Code Online (Sandbox Code Playgroud)

编辑与进展:

我仔细阅读了Matt West提供的链接中的教程.他们非常有帮助.我几乎把这弄明白了.

当Designer尝试显示示例数据时,我遇到了问题.我的样本属性出现了这个错误:Writable property or collection expected.我理解错误,我只是不知道如何修复它而不给每个属性一个set声明.

这是我的代码:

人员类:

public class Person : INotifyPropertyChanged
{
    private string _FirstName;
    private string _LastName;

    public string FirstName
    {
        get { return this._email; }
        set
        {
            this._FirstName = value;
            OnPropertyChanged("FirstName");
            OnPropertyChanged("FirstNameView");
        }
    }
    public string LastName
    {
        get { return this._password; }
        set
        {
            this._LastName = value;
            OnPropertyChanged("LastName");
            OnPropertyChanged("FirstNameView");
        }
    }
    public string FirstNameView
    {
        get { return this.FirstName; }
    }
    public string LastNameView
    {
        get { return this.LastName; }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName) { /*...*/ }
}
Run Code Online (Sandbox Code Playgroud)

PeopleViewSampleData.xaml:

<sample:People xmlns:sample="clr-namespace:MyProgram">
    <sample:Person FirstNameView="John" LastNameView="Doe" />
</sample:People>  
Run Code Online (Sandbox Code Playgroud)

编辑,SOLUTION:
ItemSource,DataContext:DataContext设置如下使它工作.如果缺少其中任何一个,则不会显示任何内容.

<ListView ItemsSource="{Binding}"
          DataContext="{Binding}"
          d:DataContext="{d:DesignData Source=/SampleData/PeopleListViewSampleData.xaml}"/>
Run Code Online (Sandbox Code Playgroud)

Mat*_*est 0

我认为您不需要在人员列表视图上设置 DataContext 并设置 ItemsSource。摆脱 DataContext={Binding}。不确定这是否能解决您的设计问题。一般来说,为此使用 d:DesignData 和 d:DesignInstance 属性通常是一个好主意。这是一个教程: http://karlshifflett.wordpress.com/2009/10/28/ddesigninstance-ddesigndata-in-visual-studio-2010-beta2/