如何在 VS WPF Design 面板中显示 Collection 数据?

发布于 2024-11-05 07:04:36 字数 3002 浏览 0 评论 0原文

如何让 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>

COLLECTION:

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" });
    }
}

MAIN:

private People PList;

public MainWindow()
{
    this.PList = new People();
    PeopleListView.ItemsSource = this.PList;
}

编辑&进展:

我通读了 Matt West 提供的链接中的教程。他们非常有帮助。我差一点就明白了。

当设计器尝试显示示例数据时,我遇到了问题。我的示例属性上出现此错误:预期可写属性或集合。我理解这个错误,我只是不知道如何在不给每个属性提供 set 语句的情况下修复它。

这是我的代码:

Person 类:

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) { /*...*/ }
}

PeopleViewSampleData.xaml:

<sample:People xmlns:sample="clr-namespace:MyProgram">
    <sample:Person FirstNameView="John" LastNameView="Doe" />
</sample:People>  

编辑,解决方案:
按如下方式设置 ItemSourceDataContext:DataContext 使其正常工作。如果缺少其中任何一个,就不会显示任何内容。

<ListView ItemsSource="{Binding}"
          DataContext="{Binding}"
          d:DataContext="{d:DesignData Source=/SampleData/PeopleListViewSampleData.xaml}"/>

How do I get VS to populate my ListView with sample data in the Design panel?

I'm stumped. I've done a fair bit of reading and googling and can't find a solid answer. Showing fake data in the VS Design panel would greatly speed up theming as I wouldn't have to debug it every few changes.

Here is my code:

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>

COLLECTION:

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" });
    }
}

MAIN:

private People PList;

public MainWindow()
{
    this.PList = new People();
    PeopleListView.ItemsSource = this.PList;
}

EDIT & PROGRESS:

I read through the tutorials found in the link provided by Matt West. They were very helpful. I almost got this figured out.

I ran into a problem when the Designer tries to display the Sample Data. I get this error on my sample properties: Writable property or collection expected. I understand the error, I just don't know how to fix it without giving each property a set statement.

Here is my code:

Person Class:

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) { /*...*/ }
}

PeopleViewSampleData.xaml:

<sample:People xmlns:sample="clr-namespace:MyProgram">
    <sample:Person FirstNameView="John" LastNameView="Doe" />
</sample:People>  

EDIT, SOLUTION:
Having ItemSource, DataContext, and :DataContext setup as follows made it work. If any of that was missing, nothing would show.

<ListView ItemsSource="{Binding}"
          DataContext="{Binding}"
          d:DataContext="{d:DesignData Source=/SampleData/PeopleListViewSampleData.xaml}"/>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡忘如思 2024-11-12 07:04:36

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

I don't think you need to set DataContext on the people list view AND set the ItemsSource. Get rid of the DataContext={Binding}. Not sure if this will solve your Design problem or not. In general its usually a good idea to use the d:DesignData and d:DesignInstance properties for this though. Here is a tutorial:
http://karlshifflett.wordpress.com/2009/10/28/ddesigninstance-ddesigndata-in-visual-studio-2010-beta2/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文