如何使用单选按钮排列列表框中的项目

时间:2014-03-06 20:56:57

标签: c# wpf listbox radio-button

好的,所以我希望能够在单击单选按钮'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;
    }
}

}


我有一张图片,但它不会让我发布


实际上,这是xaml的代码

<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>

使用单选按钮

enter image description here

1 个答案:

答案 0 :(得分:0)

更改您的IObservableCollection以仅公开您想要的结果,而不是所有结果,所以:

ObservableCollection<Create> results = new ObservableCollection<Create>();
lbxInfo.ItemsSource = results;

然后保留所有项目的集合:

// naming variables is not my strong point...
private IList<Create> _allPlayersAndManagers = new List<Create>();
_allPlayersAndManagers.Add(new Create("John Terry", 27, Create.Job.Player);

现在,您只需在选中相应按钮时填充结果,方法是使用每个按钮的Checked事件:

<RadioButton x:Name="rbtnPlayers" Content="Players" Checked="OnPlayersChecked"/>

然后按原样处理:

private void OnPlayersChecked(object source, RoutedEventArgs e)
{
    // must use the same observable instance as the binding uses
    // clear any existing entries
    results.Clear();

    // only care about players...
    foreach (var player in _allPlayersAndManagers.Where(c => c.Job == c.Job.Player))
    {
        results.Add(player);
    }
}