1

谁能解释为什么这表现得很奇怪?

它应该做的是增加一个计数并将其输出到行上,一次一个,每 3 开始一个新行。即

1

1,2

1,2,3

1,2,3 4

1,2,3 4,5

1,2,3 4,5,6

正在做的是:

1

1

1

1 4

1,2,3 4,5

然后它按预期进行,即

1,2,3 4,5,6

它在带有 ListBox 的 Windows Phone 7 中按预期工作,但在 Windows Phone 8 中却很奇怪。

我需要这个嵌套的集合构造,因此需要手动触发 OnCollectionChanged。

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <phone:LongListSelector HorizontalAlignment="Left" Height="524" Margin="10,10,0,0" VerticalAlignment="Top" Width="446" Name="listBox1">
            <phone:LongListSelector.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}"/>
                </DataTemplate>
            </phone:LongListSelector.ItemTemplate>
        </phone:LongListSelector>
        <Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="164,539,0,-4" Click="Button_Click"/>
    </Grid>
using System.Linq;
using System.Windows;
using Microsoft.Phone.Controls;
using System.Collections.Specialized;
using System.Collections.ObjectModel;

namespace ObservableCollection
{
    public partial class MainPage : PhoneApplicationPage
    {
        private Rounds rounds = new Rounds();
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            listBox1.ItemsSource = rounds;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            rounds.Add();
        }

        public class Round : Collection<int>
        {
            public string Name
            {
                get
                {
                    string s = "";
                    foreach (int i in this)
                    {
                        if (s.Length > 0)
                            s += ",";
                        s += i;
                    }
                    return s;
                }
            }
        }

        public class Rounds : ObservableCollection<Round>
        {
            public void Add()
            {
                int i = 1;

                if (Count > 0)
                    i = this.Last().Last() + 1;

                if (Count == 0 || this.Last().Count > 2)
                    Add(new Round());

                this.Last().Add(i);

                OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
            }
        }
    }
}
4

0 回答 0