【问题标题】:Sorting WPF ListView with System.Linq.IGrouping<string,Class T>使用 System.Linq.IGrouping<string,Class T> 对 WPF ListView 进行排序
【发布时间】:2009-03-23 21:16:30
【问题描述】:

实际上我在对普通 ListView.ItemsSource 进行排序时没有遇到问题,当我将 IGrouping 列表作为 ItemSource 或 SelectedItems 时,我的问题实际上出现了,我使用以下 sn-p:

    private void Sort(string sortBy, ListSortDirection direction)
    {
        lastDirection = direction;
        ICollectionView dataView = CollectionViewSource.GetDefaultView(this.ItemsSource);

        dataView.SortDescriptions.Clear();
        SortDescription sd = new SortDescription(sortBy, direction);
        dataView.SortDescriptions.Add(sd);
        dataView.Refresh();
    }

当 sortBy = 某个 ListView 列时,方向 = 升序、降序(取决于最后的排序)

那么如何设置 ItemSource 如下所示:IList when T = myClass

提前致谢!

更新:

修改了上面的sn-p:

private void Sort(string sortBy, ListSortDirection direction)
{
    lastDirection = direction;
    ICollectionView dataView;
    if (this.Name.Equals("dlstPlantillas"))
    {
        List<Plantilla> newItemSource = new List<Plantilla>();
        var source = this.ItemsSource;

        foreach (var group in source)
        {
            System.Linq.IGrouping<string, Plantilla> groupCast = group as System.Linq.IGrouping<string, Plantilla>;
            if (null == groupCast) return;
            foreach (Plantilla item in groupCast)
            {
                newItemSource.Add(item);
            }
        }
         dataView = CollectionViewSource.GetDefaultView(newItemSource);
    }
    else
    {
        dataView = CollectionViewSource.GetDefaultView(this.ItemsSource);
    }          

    dataView.SortDescriptions.Clear();
    SortDescription sd = new SortDescription(sortBy, direction);
    dataView.SortDescriptions.Add(sd);
    dataView.Refresh();
}

实际上我得到了这个堆栈:

System.ObjectDisposedException was unhandled by user code 
   Message = "Unable to access the deleted object. \ R \ nNombre object: 'It has a DataContext accessed after Dispose..'" 
   Source = "System.Data.Linq" 
   ObjectName = "It has been a DataContext accessed after Dispose." 
   Stacktrace: 
        in System.Data.Linq.DataQuery `1.System.Collections.IEnumerable.GetEnumerator () 
        in SRIMedico.com.SortableListView.Sort (String Sort, ListSortDirection direction) in D: \ cs_InformeMedico \ app \ SortableListView.cs: line 219 
        in SRIMedico.com.SortableListView.GridViewColumnHeaderClickedHandler (Object sender, RoutedEventArgs e) in D: \ cs_InformeMedico \ app \ SortableListView.cs: line 163 
        in System.Windows.RoutedEventHandlerInfo.InvokeHandler (Object target, RoutedEventArgs routedEventArgs) 
        in System.Windows.EventRoute.InvokeHandlersImpl (Object source, RoutedEventArgs args, Boolean reRaised) 
        in System.Windows.UIElement.RaiseEventImpl (DependencyObject sender, RoutedEventArgs args) 
        in System.Windows.UIElement.RaiseEvent (RoutedEventArgs e) 
        in System.Windows.Controls.Primitives.ButtonBase.OnClick () 
        in System.Windows.Controls.GridViewColumnHeader.ClickImplement () 
        in System.Windows.Controls.GridViewColumnHeader.OnClick () 
        in System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp (MouseButtonEventArgs e) 
        in System.Windows.Controls.GridViewColumnHeader.OnMouseLeftButtonUp (MouseButtonEventArgs e) 
        in System.Windows.UIElement.OnMouseLeftButtonUpThunk (Object sender, MouseButtonEventArgs e) 
        in System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler (Delegate genericHandler, Object genericTarget) 
        in System.Windows.RoutedEventArgs.InvokeHandler (Delegate handler, Object target) 
        in System.Windows.RoutedEventHandlerInfo.InvokeHandler (Object target, RoutedEventArgs routedEventArgs) 
        in System.Windows.EventRoute.InvokeHandlersImpl (Object source, RoutedEventArgs args, Boolean reRaised) 
        in System.Windows.UIElement.ReRaiseEventAs (DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent) 
        in System.Windows.UIElement.CrackMouseButtonEventAndReRaiseEvent (DependencyObject sender, MouseButtonEventArgs e) 
        in System.Windows.UIElement.OnMouseUpThunk (Object sender, MouseButtonEventArgs e) 
        in System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler (Delegate genericHandler, Object genericTarget) 
        in System.Windows.RoutedEventArgs.InvokeHandler (Delegate handler, Object target) 
        in System.Windows.RoutedEventHandlerInfo.InvokeHandler (Object target, RoutedEventArgs routedEventArgs) 
        in System.Windows.EventRoute.InvokeHandlersImpl (Object source, RoutedEventArgs args, Boolean reRaised) 
        in System.Windows.UIElement.RaiseEventImpl (DependencyObject sender, RoutedEventArgs args) 
        in System.Windows.UIElement.RaiseEvent (RoutedEventArgs args, Boolean trusted) 
        in System.Windows.Input.InputManager.ProcessStagingArea () 
        in System.Windows.Input.InputManager.ProcessInput (InputEventArgs input) 
        in System.Windows.Input.InputProviderSite.ReportInput (InputReport inputReport) 
        in System.Windows.Interop.HwndMouseInputProvider.ReportInput (IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 and Int32 wheel) 
        in System.Windows.Interop.HwndMouseInputProvider.FilterMessage (IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean & handled) 
        in System.Windows.Interop.HwndSource.InputFilterMessage (IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean & handled) 
        in MS.Win32.HwndWrapper.WndProc (IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean & handled) 
        in MS.Win32.HwndSubclass.DispatcherCallbackOperation (Object o) 
        in System.Windows.Threading.ExceptionWrapper.InternalRealCall (Delegate callback, Object args, Boolean isSingleParameter) 
        in System.Windows.Threading.ExceptionWrapper.TryCatchWhen (Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler) 
        in System.Windows.Threading.Dispatcher.WrappedInvoke (Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler) 
        in System.Windows.Threading.Dispatcher.InvokeImpl (DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter) 
        in System.Windows.Threading.Dispatcher.Invoke (DispatcherPriority priority, Delegate method, Object arg) 
        in MS.Win32.HwndSubclass.SubclassWndProc (IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) 
        in MS.Win32.UnsafeNativeMethods.DispatchMessage (MSG & msg) 
        in System.Windows.Threading.Dispatcher.PushFrameImpl (DispatcherFrame frame) 
        in System.Windows.Threading.Dispatcher.PushFrame (DispatcherFrame frame) 
        in System.Windows.Window.ShowHelper (Object booleanBox) 
        in System.Windows.Window.Show () 
        in System.Windows.Window.ShowDialog () 
   InnerException:

有这么难吗? :S

【问题讨论】:

    标签: wpf sorting listview


    【解决方案1】:

    查看堆栈跟踪表明您正在使用 LINQ-to-SQL 查询作为列表框的数据源。看来您随后处置了在其上进行查询的 DataContext。当您在 DataView 上执行 Refresh() 时,它会尝试重新运行查询,但由于 DataContext 已释放而失败。

    我想您需要做的是在将查询结果绑定到 ItemsControl 之前将其捕获到列表中。您可以使用 ToList() 扩展方法来执行此操作。

    【讨论】:

      猜你喜欢
      • 2020-11-18
      • 1970-01-01
      • 2019-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-12
      相关资源
      最近更新 更多