如何从WP8中的listBox中删除项目

时间:2014-03-02 18:31:03

标签: c# xaml windows-phone-8 listboxitem

我在Windows手机中有一个列表框视图,我想在指定的事件中从中删除一个项目....我怎么能实现这一点 这是我的XAML代码

 <ListBox x:Name="lstData2"
    ItemsSource="{Binding DataCollection2, Source={StaticResource PinsCollection}}" Grid.ColumnSpan="3" Foreground="#FF1D53D0" Height="583" VerticalAlignment="Bottom">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" Tap="StackPanel_Tap">
                <Image Margin="8"
                    VerticalAlignment="Top"
                    Source="{Binding ImageUri}"
                    Width="100"
                    Height="100" />
                <StackPanel Height="93" Width="259" >
                    <TextBlock Margin="8"
                        Width="250"

                        TextWrapping="Wrap"
                        VerticalAlignment="Top"
                        HorizontalAlignment="Left"
                        Foreground="#FF1D53D0"
                        Text="{Binding Pinnedname}"  Height="33" RenderTransformOrigin="0.5,0.5" FontFamily="Segoe WP SemiLight" FontSize="24" FontWeight="Bold" />
                    <TextBlock Width="155"
                        Margin="8,0,8,8"
                        VerticalAlignment="Top"
                        HorizontalAlignment="Left"
                        Text="{Binding Status}" Foreground="#FF1D53D0" FontFamily="Segoe WP SemiLight" />
                    <toolkit:ContextMenuService.ContextMenu>
                        <toolkit:ContextMenu>
                            <toolkit:MenuItem Header="Remove Pin" Click="RemovePin_Click" Tag="{Binding pinId}"/>

                        </toolkit:ContextMenu>
                    </toolkit:ContextMenuService.ContextMenu>
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

1 个答案:

答案 0 :(得分:0)

您正在使用绑定在列表视图中显示数据(DataCollection2)。修改数据后,列表视图会更新。要删除ListView中的项目,应删除DataCollection2中的相应项目,列表视图将自动更新。确保DataCollection2是一个ObservableCollection实例。