删除数据源绑定

时间:2012-10-10 10:09:39

标签: c# xaml windows-8

我有一个这样的页面

<Page.Resources>
    <!--
        這個頁面顯示的群組項目集合,繫結到完整項目
        清單的子集,因為群組中的項目無法虛擬化
    -->
    <CollectionViewSource
        x:Name="groupedItemsViewSource"
        Source="{Binding Groups}"
        IsSourceGrouped="true"
        ItemsPath="TopItems"
        d:Source="{Binding AllGroups, Source={d:DesignInstance Type=data:SampleDataSource, IsDesignTimeCreatable=True}}"/>

    <!-- TODO: 如果已在 App.xaml 中宣告金鑰 AppName,則刪除這一行 -->
    <x:String x:Key="AppName">我的應用程式</x:String>
</Page.Resources>

    <local:MyGridView            
        Grid.Row="1"
        x:Name="itemGridView"
        VerticalAlignment="Top"
        Padding="116,0,40,0"
        ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}"                    
        SelectionMode="Multiple"
        IsSwipeEnabled="True"
        IsRightTapEnabled="False"
        AutomationProperties.AutomationId="ItemGridView"
        AutomationProperties.Name="Items"
        ScrollViewer.IsHorizontalScrollChainingEnabled="False"                    
        IsItemClickEnabled="True"
        ItemTemplate="{StaticResource Standard180x240ItemTemplate}"
        SelectionChanged="itemGridView_SelectionChanged_1"
        ItemClick="ItemView_ItemClick">

        <local:MyGridView.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </local:MyGridView.ItemsPanel>
        <local:MyGridView.GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <Grid Margin="1,0,0,6" >
                            <Button AutomationProperties.Name="Group Title" Style="{StaticResource TextPrimaryButtonStyle}" >
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="{Binding Title}" Margin="3,-7,10,10" Style="{StaticResource GroupHeaderTextStyle}" />
                                </StackPanel>
                            </Button>
                        </Grid>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
                <GroupStyle.Panel>
                    <ItemsPanelTemplate>
                        <!--<VariableSizedWrapGrid Orientation="Vertical" Margin="0,0,80,0"/>-->
                        <VariableSizedWrapGrid MaximumRowsOrColumns="8" ItemWidth="180" ItemHeight="240" Orientation="Vertical" Margin="0,0,80,0"/>
                    </ItemsPanelTemplate>
                </GroupStyle.Panel>
            </GroupStyle>
        </local:MyGridView.GroupStyle>
    </local:MyGridView>

当我尝试删除以下代码中的所选项目时:

private void Delete()
{
    var group = SampleDataSource.GetGroup("myGroup");
    foreach (SampleDataItem item in itemGridView.SelectedItems)
    {
        group.Items.Remove(item);
    }
}

我在App.gi.cs文件中收到错误

 #if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
        UnhandledException += (sender, e) =>
        {
            if (global::System.Diagnostics.Debugger.IsAttached)     
                global::System.Diagnostics.Debugger.Break();
        };
 #endif

e.Message显示:

“同一个包含Storyboard的多个动画无法在单个元素上定位相同的属性。”串

那么如何删除所选项目呢?

谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

我真的很想知道为什么会这样,但我什么都没发现。但我有一个解决方案:

private void Delete()
{
    IList<SampleDataItem> selectedItems = new List<SampleDataItem>();
    foreach (SampleDataItem item in itemGridView.SelectedItems)
    {
        selectedItems.Add(item);
    }
    var group = SampleDataSource.GetGroup("myGroup");
    foreach (SampleDataItem item in selectedItems)
    {
        group.Items.Remove(item);
    }
}

如果您发现“为什么”,请告诉我们! :)

答案 1 :(得分:0)

我面前没有编译器,但你尝试过类似的东西:

group.Items.RemoveAll(itemGridView.SelectedItems);

而不是你的foreach循环。您可能会发现SelectedItems和group之间存在交叉依赖性问题(在循环外部删除也是如此)。