通过按钮删除列表框中的项目

时间:2012-12-21 16:30:42

标签: wpf templates .net-4.0 listbox wpf-controls

我有一个代表歌曲的模板:

<DataTemplate x:Key="songTemplate" DataType="local:song">
        <Border BorderBrush="Gray" BorderThickness="1" Padding="2">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto"/>
                    <RowDefinition Height="auto"/>
                    <RowDefinition Height="auto"/>
                </Grid.RowDefinitions>
                <Grid Name="songGrid" Grid.Row="0">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto"/>
                        <RowDefinition Height="auto"/>
                        <RowDefinition Height="auto"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="auto"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Column="0" Grid.Row="0" Text="title: "/>
                    <TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding Path=title}"/>
                    <TextBlock Grid.Column="2" Grid.Row="0" Text="artist: "/>
                    <TextBlock Grid.Column="3" Grid.Row="0" Text="{Binding Path=artist}"/>
                    <TextBlock Grid.Column="0" Grid.Row="1" Text="path: "/>
                    <TextBlock Grid.Column="1" Grid.Row="1" Text="{Binding Path=path}"/>
                    <TextBlock Grid.Column="2" Grid.Row="1" Text="status: "/>
                    <TextBlock Grid.Column="3" Grid.Row="1" Text="{Binding Path=stat}"/>
                </Grid>
                <Grid Grid.Row="1">
                    <Expander Margin="2" Padding="2" BorderBrush="Gray" BorderThickness="1" ExpandDirection="Down" >
                        <TextBlock Width="400" Text="{Binding Path=statInfo}" TextWrapping="Wrap"/>
                    </Expander>
                </Grid>
                <Grid Grid.Row="2">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Button VerticalAlignment="Center" Grid.Column="0">
                        <Image Source="pictures/check.gif"/>
                    </Button>
                    <Button Name="Cancel" VerticalAlignment="Center" Grid.Column="1" Click="DontSave">
                        <Image Source="pictures/cross.gif"/>
                    </Button>
                </Grid>
            </Grid>
        </Border>
    </DataTemplate>

我正在我的代码中添加歌曲到observebalecollction,并且歌曲在列表框中更新。 模板有两个按钮。其中一个需要从列表中删除歌曲。 我正在尝试找到一种方法从列表中删除列表中所选项目的歌曲,但是当单击该按钮时,该项目未被选中。

我怎么能删除它?

1 个答案:

答案 0 :(得分:0)

尝试使用ClickEventHandler:

    void DontSave(object sender, RoutedEventArgs e)
    {
        song s = ((FrameworkElement)sender).DataContext as song;
        if (s != null && songCollection.Contains(s)) songCollection.Remove(s);
    }