将列表绑定到列表框
本文关键字:列表 绑定 | 更新日期: 2023-09-27 18:35:45
我有一个 wpf 应用程序。 它有一个称为 reports 的用户控件和几个类,当我运行命令时,我想将用户控件上的列表框更改为该列表。
我已经尝试过列表和列表。我真正想要的是能够拥有一个列表并使用它。你能告诉我在哪里可以找到如何做到这一点,或者解释我应该寻找什么吗?
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"}
};
}
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; }
}
}