использование observableCollection в longlistselector - PullRequest
0 голосов
/ 11 декабря 2011

Я использую _wordItems в качестве источника элементов longlistselector для динамического удаления элементов в longlistSelector

private ObservableCollection<Group<WordItem>> _wordItems = new ObservableCollection<Group<WordItem>>();

Класс группы определяется как:

public class Group<T> : ObservableCollection<T>
{
    public Group(string name, IEnumerable<T> items)
    {
        this.Key = name;
        foreach (T item in items)
        {
            this.Add(item);
        }
    }

    public override bool Equals(object obj)
    {
        Group<T> that = obj as Group<T>;
        return (that != null) && (this.Key.Equals(that.Key));
    }

    public string Key
    {
        get;
        set;
    }
}

, а класс WordItem -определяется как:

[Table]
public class WordItem //:INotifyPropertyChanged,INotifyPropertyChanging
{
    private int _wordItemId;
    [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
    public int WordItemId
    {
        get
        {
            return _wordItemId;
        }
        set
        {
            if (_wordItemId != value)
            {
                _wordItemId = value;
            }
        }
    }

    private string _word;
    [Column(CanBeNull=false)]
    public string Word
    {
        get
        {
            return _word;
        }
        set
        {
            if (_word != value)
            {
                _word = value;
            }
        }
    }

    private string _wordExplains;
    [Column]
    public string WordExplains
    {
        get
        {
            return _wordExplains;
        }
        set
        {
            if (_wordExplains != value)
            {
                _wordExplains = value;
            }
        }
    }
}

, когда я использую код _wordItems[1].RemoveAt(1);, _wordItems был изменен, и longlistselector на экране также удалил определенный элемент.Но проблема в том, что когда я вызываю _wordItems[1].RemoveAt(1);, longlistselector только что удалил элемент _wordItems[1][0].Когда я звоню _wordItems[1].RemoveAt(0);, он просто удаляет заголовок второй группы и объединяет элементы в _worditems[1] с _wordItem[0].

Определение longlistselector:

<toolkit:LongListSelector x:Name="WordsGroup" Background="Transparent"
                                              Margin="12,0,12,73" ItemTemplate="{StaticResource WordItemTemplate}"
                                              GroupHeaderTemplate="{StaticResource YoudaoGroupHeaderTemplate}"
                                              GroupItemTemplate="{StaticResource YoudaoGroupItemTemplate}" 
                                              SelectionChanged="WordsGroup_SelectionChanged"
                                              >
                        <toolkit:LongListSelector.GroupItemsPanel>
                            <ItemsPanelTemplate>
                                <toolkit:WrapPanel/>
                            </ItemsPanelTemplate>
                        </toolkit:LongListSelector.GroupItemsPanel>
                    </toolkit:LongListSelector>

Таблица данных имеет вид:

   <DataTemplate x:Key="WordItemTemplate">
            <Border BorderBrush="Gray" BorderThickness="0,0,0,0" Margin="0,3,0,5">
                <StackPanel >
                    <toolkit:ContextMenuService.ContextMenu>
                        <toolkit:ContextMenu Opened="ContextMenu_Opened" >
                            <toolkit:MenuItem Header="delete this word" Click="DelectWord_Click"/>
                            <toolkit:MenuItem Header="share this word" Click="SMSShare_Click"/>
                        </toolkit:ContextMenu>
                    </toolkit:ContextMenuService.ContextMenu>
                    <TextBlock Text="{Binding WordItemId}" Style="{StaticResource PhoneTextNormalStyle}" Visibility="Collapsed"/>
                    <TextBlock Text="{Binding Word}" Style="{StaticResource PhoneTextNormalStyle}" FontSize="25" Foreground="Black" FontWeight="Bold"/>
                    <TextBlock Text="{Binding WordExplains}" Style="{StaticResource PhoneTextNormalStyle}"  Foreground="Black" TextWrapping="Wrap"/>
                </StackPanel>
            </Border>

        </DataTemplate>

        <DataTemplate x:Key="YoudaoGroupHeaderTemplate">
            <Border Background="#1BA1E2" Margin="0">
                <TextBlock Text="{Binding Key}" FontSize="30" Margin="12,0,0,0" Foreground="Black" />
            </Border>
        </DataTemplate>

        <DataTemplate x:Key="YoudaoGroupItemTemplate" >
            <Border Background="#1BA1E2" Width="99" Height="99" Margin="6">
                <TextBlock Text="{Binding Key}" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="40" Foreground="{StaticResource PhoneForegroundBrush}"/>
            </Border>
        </DataTemplate>

Заранее.

пс.Я использую linq как

var wordBy4sLetter = from word in wordList
                                         group word by word.Word.ToCharArray()[0].ToString() into s
                                         orderby s.Key
                                         select new Group<WordItem>(s.Key, s);
this._wordItems = wordBy4sLetter.ToObservableCollection();
                        this.WordsGroup.ItemsSource = this._wordItems;

1 Ответ

0 голосов
/ 13 декабря 2011

Я думаю, вам нужен еще один ObservableCollection.Посмотрите на принятый ответ на этот вопрос. Список групп для Windows Phone 7?

Я изменил там код и создал новые методы добавления / удаления, которые создадут группу, если ее там нет, и удалите группу, еслипусто при удалении.

...