排序gridview WPF后如何访问绑定资源

时间:2010-10-16 15:38:32

标签: wpf sorting binding

我将一个可观察的集合绑定到我的gridview。

    public ObservableCollection<LibraryTrack> Biblio
    { get { return _Biblio; } }

gridview只包含用户要查看的必要值。像filelocation和file id这样的辅助信息没有绑定到gridview,在这种情况下,这个信息对于用户来说是无用的。

首先是一些代码:

    <GridView x:Key="gridview">
        <GridViewColumn Width="200" 
                                        HeaderContainerStyle="{StaticResource hcs}"
                                        DisplayMemberBinding="{Binding Title}">
            <GridViewColumnHeader Content="Titel" Tag="Title" Click="SortClick"/>
        </GridViewColumn>
        <GridViewColumn Width="200" 
                                        HeaderContainerStyle="{StaticResource hcs}"
                                        DisplayMemberBinding="{Binding Artist}">
            <GridViewColumnHeader Content="Artiest" Tag="Artist" Click="SortClick"/>
        </GridViewColumn>
        <GridViewColumn Width="200" 
                                        HeaderContainerStyle="{StaticResource hcs}"
                                        DisplayMemberBinding="{Binding Album}">
            <GridViewColumnHeader Content="Album" Tag="Album" Click="SortClick"/>
        </GridViewColumn>
        <GridViewColumn Width="50" 
                                        HeaderContainerStyle="{StaticResource hcs}"
                                        DisplayMemberBinding="{Binding Genre}">
            <GridViewColumnHeader Content="Genre" Tag="Genre" Click="SortClick"/>
        </GridViewColumn>
        <GridViewColumn Width="50" 
                                        HeaderContainerStyle="{StaticResource hcs}"
                                        DisplayMemberBinding="{Binding Jaar}">
            <GridViewColumnHeader Content="Jaar" Tag="Jaar" Click="SortClick"/>
        </GridViewColumn>
    </GridView>

通过代码启用排序并完美运行。在后面的代码中,我有以下事件处理程序:

    private void SortClick(object sender, RoutedEventArgs e)
    {
        GridViewColumnHeader column = sender as GridViewColumnHeader;
        String field = column.Tag as String;

        if (_CurSortCol != null)
        {
            AdornerLayer.GetAdornerLayer(_CurSortCol).Remove(_CurAdorner);
            LibView.Items.SortDescriptions.Clear();
        }
        ListSortDirection newDir = ListSortDirection.Ascending;
        if (_CurSortCol == column && _CurAdorner.Direction == newDir)
            newDir = ListSortDirection.Descending;

        _CurSortCol = column;
        _CurAdorner = new SortAdorner(_CurSortCol, newDir);
        AdornerLayer.GetAdornerLayer(_CurSortCol).Add(_CurAdorner);
        LibView.Items.SortDescriptions.Add(new SortDescription(field, newDir));   
    }

Libview是包含gridview的ListView。要开始播放所选歌曲我使用下一个代码:

    private void BtnPlay_Click(object sender, RoutedEventArgs e)
    {
        if (LibView.SelectedIndex == -1)
        {
            LibView.SelectedIndex = 0;
        }
            LibraryTrack curtrack = Biblio[LibView.SelectedIndex];
            songInfoLabel1.Text = curtrack.Title + "\n" +
                                  curtrack.Artist + "\n" +
                                  curtrack.Album + "\n" +
                                  curtrack.Jaar;
            MediaEl.Source = new Uri(curtrack.Location);
            MediaEl.Play();    
    }

这是我的问题。只要gridview没有排序Btn_Play_Click处理一切就好了。

但是当我对行进行排序时,Btn_Play_Click会启动错误的歌曲。 我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

不要使用Biblio[LibView.SelectedIndex]来获取所选的LibraryTrack,而只需使用LibView.SelectedItem

LibraryTrack curtrack = LibView.SelectedItem as LibraryTrack;