Home Dashboard Directory Help
Search

CollectionViewSource sorting only the first time it is bound to a source by Mischiew


Status: 

Closed


12
0
Sign in
to vote
Type: Bug
ID: 592897
Opened: 9/1/2010 2:02:36 AM
Access Restriction: Public
2
Workaround(s)
view
10
User(s) can reproduce this bug

Description

I'm using a DataGrid bound to a CollectionViewSource (players), itself bound to the currently selected item of a ListBox (levels), each item containing a collection to be sorted/displayed in the DataGrid (see attachment for a small test project).

The problem is that the players are sorted the first time they are shown, but as soon as I select another level from the ListBox, they are not sorted anymore. Also, modifying names the first time players are shown will sort them accordingly to the changes, but not anymore once the level has been changed.
Details
Sign in to post a comment.
Posted by NetDev32 on 2/27/2013 at 12:12 PM
The work around by Greg Bachraty does not "Cut" the code by overriding the metadata. Overriding does not change the behavior of the parent class and the OnCoerceItemsSourceProperty method is still called (Clearing the items Sorts/ Group).

Therefore that workaround will not prevent this issue.
Posted by Microsoft on 11/15/2012 at 2:29 PM
The WPF team has recently reviewed this issue and will not be addressing this issue as at this time the team is focusing on the bugs impacting the highest number of WPF developers. If you believe that this was resolved in error, please reactivate this bug with any necessary supporting details.

We appreciate the feedback. However, this issue will not be addressed in the next version of WPF. Thank you.
–WPF Team.
Posted by LeftosGIAA on 7/16/2012 at 7:07 AM
Added another workaround for this that seems to work fine.
Posted by LeftosGIAA on 7/16/2012 at 6:55 AM
After all this time there's still no fix for this?

It's verified all over the place, including StackOverflow.
Posted by Mischiew on 2/28/2011 at 5:03 AM
Thanks MS for the good support. Bottom line, don't use the DataGrid...
Posted by Greg Bachraty on 1/21/2011 at 2:10 PM
I feel your pain. I've been scratching my head for a viable workaround for days now. Bugs like this are the reason WPF is still not ready for production-quality business apps even though WinForms data binding is far from perfect.
Posted by Mischiew on 1/2/2011 at 4:42 AM
Feedback would be welcome, will this bug be fixed one day?
The DataGrid is still pretty much unstable, it would be good to know whether to use it at all in current applications.
Posted by Mischiew on 10/30/2010 at 2:40 AM
Any progress on this bug?
Posted by Microsoft on 9/1/2010 at 2:12 AM
Thanks for your feedback.

We are rerouting this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.
Posted by Mischiew on 9/1/2010 at 2:11 AM
The error is in the DataGrid. If I'm using a ListBox instead of a DataGrid, the problem disappears. If I'm using a DataGrid and a ListBox (to double-check), then the problem is back (see commented code in MainWindow.xaml).

The source of the problem is most likely this one in the DataGrid sources:

1) in the static constructor:

ItemsSourceProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata((PropertyChangedCallback)null, OnCoerceItemsSourceProperty));

2) lower:

        /// <summary>
        ///     Coercion callback for ItemsSource property
        /// </summary>
        /// <remarks>
        ///     SortDescriptions and GroupDescriptions are supposed to be
        ///     cleared in PropertyChangedCallback or OnItemsSourceChanged
        ///     virtual. But it seems that the SortDescriptions are applied
        ///     to the new CollectionView due to new ItemsSource in
        ///     PropertyChangedCallback of base class (which would execute
        ///     before PropertyChangedCallback of this class) and before calling
        ///     OnItemsSourceChanged virtual. Hence handling it in Coercion callback.
        /// </remarks>
        private static object OnCoerceItemsSourceProperty(DependencyObject d, object baseValue)
        {
            DataGrid dataGrid = (DataGrid)d;
            if (baseValue != dataGrid._cachedItemsSource && dataGrid._cachedItemsSource != null)
            {
                dataGrid.ClearSortDescriptionsOnItemsSourceChange();
            }

            return baseValue;
        }


If the descriptions are removed, clearly it will break up everything each time another source is selected!
Posted by Mischiew on 9/1/2010 at 2:06 AM
As one can see in the MainWindow.xaml.cs file, I've tried a workaround to save and replace the SortDescription items in the CollectionView, but while this fixes the displayed order, it breaks the keyboard nivagation and it is not possible anymore to use the up/down key to get to all the items (stays stuck on some of them).
Sign in to post a workaround.
Posted by LeftosGIAA on 7/16/2012 at 7:07 AM
I've found that adding the SortDescriptions to the CollectionViewSource AFTER I bind it to the ItemsSource of the DataGrid works fine.


dgPlayerStats.ItemsSource = psrView;
psrView.SortDescriptions.Add(new SortDescription(sortColumn, ListSortDirection.Descending));
Posted by Greg Bachraty on 2/28/2011 at 6:50 AM
Implement your own DataGrid:
public class SDataGrid : DataGrid
{
    static SDataGrid()
    {
        ItemsControl.ItemsSourceProperty.OverrideMetadata(typeof(SDataGrid), new FrameworkPropertyMetadata((PropertyChangedCallback)null, (CoerceValueCallback)null));
    }
}
The only thing coerce callback does in the current implementation is clear the sort descriptions. You can simply "cut" this code by overriding metadata. Not viable on Silverlight: OverrideMetadata API is not public. Though I'm not sure Silverlight is affected by this bug. Other risks and side effects may apply.
File Name Submitted By Submitted On File Size  
DataGrid.Sort.zip 9/1/2010 9 KB