2

我可能在这里做一些微不足道的错误,但我似乎无法让以下工作:

    public MainWindow()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(MainWindow_Loaded);
    }

    public Queue<Player> quePlayers { get; set; }

    Random rand = new Random((int)DateTime.Now.Ticks);

    public void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        quePlayers = new Queue<Player>();
        //quePlayers.Add(new Player() { FullName = Guid.NewGuid().ToString(), Age = rand.Next() % 40, SomeData = Guid.NewGuid().ToString() });
        //quePlayers.Add(new Player() { FullName = Guid.NewGuid().ToString(), Age = rand.Next() % 40, SomeData = Guid.NewGuid().ToString() });
        //quePlayers.Add(new Player() { FullName = Guid.NewGuid().ToString(), Age = rand.Next() % 40, SomeData = Guid.NewGuid().ToString() });
        //quePlayers.Add(new Player() { FullName = Guid.NewGuid().ToString(), Age = rand.Next() % 40, SomeData = Guid.NewGuid().ToString() });
        //quePlayers.Add(new Player() { FullName = Guid.NewGuid().ToString(), Age = rand.Next() % 40, SomeData = Guid.NewGuid().ToString() });

        quePlayers.Enqueue(new Player() { FullName = Guid.NewGuid().ToString(), Age = rand.Next() % 40, SomeData = Guid.NewGuid().ToString() });
        quePlayers.Enqueue(new Player() { FullName = Guid.NewGuid().ToString(), Age = rand.Next() % 40, SomeData = Guid.NewGuid().ToString() });
        quePlayers.Enqueue(new Player() { FullName = Guid.NewGuid().ToString(), Age = rand.Next() % 40, SomeData = Guid.NewGuid().ToString() });
        quePlayers.Enqueue(new Player() { FullName = Guid.NewGuid().ToString(), Age = rand.Next() % 40, SomeData = Guid.NewGuid().ToString() });
        quePlayers.Enqueue(new Player() { FullName = Guid.NewGuid().ToString(), Age = rand.Next() % 40, SomeData = Guid.NewGuid().ToString() });

        dgTest.ItemsSource = quePlayers;
        //dgTest.ItemsSource = CollectionViewSource.GetDefaultView(dgTest.ItemsSource);

    }

如果我将该队列更改为列表,网格中将出现数据,但队列和堆栈将不合作

dgTest.Items 属性有 5 个成员,我得到的 CollectionView 是 EnumerableCollectionView,我可以枚举它并查看其中的 5 个成员

我的 XAML 也很简单

<DataGrid x:Name="dgTest" AutoGenerateColumns="True" />

知道有什么问题吗?Queue 实现了 IEnumerable 并且 ItemsSource 的唯一要求是 IEnumerable

4

1 回答 1

2

我的猜测是 AutoGenerateColumns。尝试手动设置一列。

于 2012-12-12T21:31:01.313 回答