0

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

<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,歌曲在列表框中更新。该模板有两个按钮。其中一个需要从列表中删除歌曲。我正在尝试找到一种方法,使用列表中的选定项目从列表中删除歌曲,但是当单击按钮时,未选择该项目。

知道如何删除它吗?

4

1 回答 1

0

为您的 ClickEventHandler 试试这个:

    void DontSave(object sender, RoutedEventArgs e)
    {
        song s = ((FrameworkElement)sender).DataContext as song;
        if (s != null && songCollection.Contains(s)) songCollection.Remove(s);
    }
于 2012-12-21T17:11:25.587 回答