开发者

How to access bound resource after sorting gridview WPF

开发者 https://www.devze.com 2023-01-20 14:52 出处:网络
I am binding an observable collection to my gridview. public ObservableCollection<LibraryTrack> Biblio

I am binding an observable collection to my gridview.

    public ObservableCollection<LibraryTrack> Biblio
    { get { retur开发者_运维知识库n _Biblio; } }

The gridview only contains the necessary values for the user to see. The secondary information like filelocation and file id isn't bound to the gridview, this info is in this case useless for the user to see.

First some code:

    <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>

Sorting is enabled via code and works perfectly. In the code behind, I have the following event handler:

    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 is the ListView containing the gridview. to start playing the selected song I use next code:

    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();    
    }

Here comes my problem. As long as the gridview isn't sorted Btn_Play_Click handles everything just fine.

But when I sort the rows the Btn_Play_Click starts the wrong song. How can I fix this?


Instead of using Biblio[LibView.SelectedIndex] to get the selected LibraryTrack, just use LibView.SelectedItem instead.

LibraryTrack curtrack = LibView.SelectedItem as LibraryTrack;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号