将List <t>绑定到Listbox

时间:2016-02-22 16:39:45

标签: c# wpf

我有一个wpf应用程序。有一个名为reports的UserControl和几个类,当我运行命令时,我想将usercontrol上的Listbox更改为该列表。

我尝试了List和List。我真正想要的是能够拥有一个List并使用它。你能告诉我在哪里找到这个,或解释我应该寻找什么。

    private List<WhatDoIMakeThis> _myList;
    public List<WhatDoIMakeThis> MyList
    {
        get { return _myList; }
        set { OnPropertyChanged(); }
    }

    private void RunReport(Report report)
    {

        switch (report)
        {
            case Report.Category:
                MyList = GetCategory();
                break;
            case Report.Person:
                MyList = GetPeople();
                break;
            case Report.Book:
                MyList = GetBooks();
                break;
            default:
                throw new ArgumentOutOfRangeException(nameof(report), report, null);
        }
    }


    List<Category> GetCategory()
    {
        var myList = new List<Category>()
        {
            new Category() {Rank = 1, CategoryName = "Book"}, new Category() {Rank = 2, CategoryName = "Person"}
        };
        return myList;
    }

    List<Person> GetPeople()
    {
        return new List<Person>()
        {
            new Person() {Name = "Jone Doe", Phone = "(317) 123 4567"}, new Person() {Name = "Jane Doe", Phone = "(317) 234 5678"}, new Person() {Name = "Kid Doe", Phone = "(317) 345 7890"},
        };
    }

    List<Books> GetBooks()
    {
        return new List<Books>()
        {
            new Books() {Auther = "John Doe", Company = "This Comapny", Name = "This Book"}
        };
    }

1 个答案:

答案 0 :(得分:0)

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1">
    <Grid>
        <ListBox Name="lb" ItemsSource="{Binding}">
            <ListBox.Resources>
                <DataTemplate DataType="{x:Type local:Books}">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Name}" Width="200"/>
                        <TextBlock Text="{Binding Auther}" Width="200"/>
                        <TextBlock Text="{Binding Company}"/>
                    </StackPanel>
                </DataTemplate>
                <DataTemplate DataType="{x:Type local:Category}">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding CategoryName}" Width="200"/>
                        <TextBlock Text="{Binding Rank}"/>
                    </StackPanel>
                </DataTemplate>
                <DataTemplate DataType="{x:Type local:Person}">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Name}" Width="200"/>
                        <TextBlock Text="{Binding Phone}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.Resources>
        </ListBox>
    </Grid>
</Window>

C#

using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        List<Person> ps = new List<Person>();
        List<Category> cs = new List<Category>();
        List<Books> bs = new List<Books>();

        public MainWindow()
        {
            InitializeComponent();

            //Bind to ps
            ps.Add(new Person { Name = "A", Phone = "000" });
            lb.DataContext = ps;

            //Bind to cs
            cs.Add(new Category { CategoryName = "cat", Rank = 1 });
            lb.DataContext = cs;

            //Bind to bs
            bs.Add(new Books { Name = "my book", Auther = "xxx", Company = "ABC" });
            lb.DataContext = bs;
        }
    }

    public class Person
    {
        public string Name { get; set; }
        public string Phone { get; set; }
    }

    public class Books
    {
        public string Name { get; set; }
        public string Auther { get; set; }
        public string Company { get; set; }

    }

    public class Category
    {
        public string CategoryName { get; set; }
        public int Rank { get; set; }
    }
}