好的,所以我希望能够在单击单选按钮'all'时显示列表框中的所有信息。我还希望能够在单击“玩家”单选按钮时“仅”显示玩家,对于经理也是如此。有帮助吗?谢谢!
class Create
{
public enum Job { Player, Manager }
public string Name { get; set; }
public int Age { get; set; }
public Job Porm { get; set; }
public Create(string name, int age, Job porm)
{
Name = name; Age = age; Porm = porm;
}
public override string ToString()
{
return string.Format("Name: {0} - Age {1} - {2}", Name, Age, Porm);
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ObservableCollection<Create> playersAndManagers;
playersAndManagers = new ObservableCollection<Create>();
Create newPlayer1 = new Create("John Terry", 27, Create.Job.Player);
Create newManager1 = new Create("jose mourinho", 51, Create.Job.Manager);
Create newPlayer2 = new Create("Wayne Rooney", 23, Create.Job.Player);
Create newPlayer3 = new Create("David Beckham", 37, Create.Job.Player);
Create newManager2 = new Create("David Moyes", 55, Create.Job.Manager);
playersAndManagers.Add(newPlayer1);
playersAndManagers.Add(newManager1);
playersAndManagers.Add(newPlayer2);
playersAndManagers.Add(newPlayer3);
playersAndManagers.Add(newManager2);
lbxInfo.ItemsSource = playersAndManagers;
}
}
<Window x:Class="example1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox x:Name="lbxInfo" HorizontalAlignment="Left" Height="207" Margin="66,71,0,0" VerticalAlignment="Top" Width="368"/>
<RadioButton x:Name="rbtnAll" Content="All" HorizontalAlignment="Left" Margin="66,28,0,0" VerticalAlignment="Top"/>
<RadioButton x:Name="rbtnPlayers" Content="Players" HorizontalAlignment="Left" Margin="203,28,0,0" VerticalAlignment="Top"/>
<RadioButton x:Name="rbtnManagers" Content="Managers" HorizontalAlignment="Left" Margin="352,28,0,0" VerticalAlignment="Top"/>
</Grid>
</Window>
答案 0 :(得分:1)
可观察的集合非常棘手,它们缺少普通List<T>
的所有奇特功能。我建议你有一个List
包含所有数据,第二个集合包含你想要显示的数据。
要显示所有数据,您只需说明
lbxInfo.ItemsSource = playersAndManagers;
当您只想显示经理时:
lbxInfo.ItemsSource = playersAndManagers.Where(m => m.Porm == Job.Manager)
仅展示球员:
lbxInfo.ItemsSource = playersAndManagers.Where(p => p.Porm == Job.Player)
如果playersAndManagers
- 列表在您的应用程序中发生更改,则不会更新显示的列表。如果您要修改应用程序中的列表并保持ListBox
更新,那么最简单的方法是创建一个具有属性的自定义列表,该列表可以返回ObservableCollection
并保持更新