我使用_wordItems作为longlistselector的itemsource,以便动态删除longlistSelector中的项目
private ObservableCollection<Group<WordItem>> _wordItems = new ObservableCollection<Group<WordItem>>();
Group类定义为:
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如下:
<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>
Thx提前。
PS。我正在使用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;
答案 0 :(得分:0)
我认为你需要另一个ObservableCollection。看一下这个问题的接受答案。 Group Listbox for Windows Phone 7?
我已修改了那里的代码,并创建了新的添加/删除方法,如果没有添加,则会创建一个组,如果删除该组,则删除该组。