Advertisement
dg9ngf

ListView Layout Manager modification

Oct 18th, 2012
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.51 KB | None | 0 0
  1.         // ADD this variable to the class:
  2.         private INotifyCollectionChanged prevListViewItemsSource;
  3.  
  4.         public ListViewLayoutManager(ListView listView)
  5.         {
  6.             if ( listView == null )
  7.             {
  8.                 throw new ArgumentNullException( "listView" );
  9.             }
  10.  
  11.             this.listView = listView;
  12.             this.listView.Loaded += new RoutedEventHandler( ListViewLoaded );
  13.             this.listView.Unloaded += new RoutedEventHandler( ListViewUnloaded );
  14.  
  15.             // ADD from the following lines and methods:
  16.             DependencyPropertyDescriptor dpd =
  17.                 DependencyPropertyDescriptor.FromProperty(ListView.ItemsSourceProperty, typeof(ListView));
  18.             dpd.AddValueChanged(this.listView, ListViewItemsSourceChanged);
  19.             ListViewItemsSourceChanged(null, null);
  20.         } // ListViewLayoutManager
  21.  
  22.         private void ListViewItemsSourceChanged(object sender, EventArgs e)
  23.         {
  24.             if (prevListViewItemsSource != null)
  25.             {
  26.                 prevListViewItemsSource.CollectionChanged -= source_CollectionChanged;
  27.             }
  28.             var source = listView.ItemsSource as INotifyCollectionChanged;
  29.             prevListViewItemsSource = source;
  30.             if (source != null)
  31.             {
  32.                 source.CollectionChanged += source_CollectionChanged;
  33.             }
  34.         }
  35.  
  36.         private void source_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
  37.         {
  38.             ResetColumnWidths();
  39.  
  40.             switch (e.Action)
  41.             {
  42.                 case NotifyCollectionChangedAction.Add:
  43.                     foreach (object o in e.NewItems)
  44.                     {
  45.                         var item = o as INotifyPropertyChanged;
  46.                         if (item != null)
  47.                         {
  48.                             item.PropertyChanged += item_PropertyChanged;
  49.                         }
  50.                     }
  51.                     break;
  52.                 case NotifyCollectionChangedAction.Remove:
  53.                     foreach (object o in e.OldItems)
  54.                     {
  55.                         var item = o as INotifyPropertyChanged;
  56.                         if (item != null)
  57.                         {
  58.                             item.PropertyChanged -= item_PropertyChanged;
  59.                         }
  60.                     }
  61.                     break;
  62.                 case NotifyCollectionChangedAction.Replace:
  63.                     foreach (object o in e.OldItems)
  64.                     {
  65.                         var item = o as INotifyPropertyChanged;
  66.                         if (item != null)
  67.                         {
  68.                             item.PropertyChanged -= item_PropertyChanged;
  69.                         }
  70.                     }
  71.                     foreach (object o in e.NewItems)
  72.                     {
  73.                         var item = o as INotifyPropertyChanged;
  74.                         if (item != null)
  75.                         {
  76.                             item.PropertyChanged += item_PropertyChanged;
  77.                         }
  78.                     }
  79.                     break;
  80.                 case NotifyCollectionChangedAction.Reset:
  81.                     var list = sender as IEnumerable;
  82.                     if (list != null)
  83.                     {
  84.                         foreach (object o in list)
  85.                         {
  86.                             var item = o as INotifyPropertyChanged;
  87.                             if (item != null)
  88.                             {
  89.                                 item.PropertyChanged += item_PropertyChanged;
  90.                             }
  91.                         }
  92.                     }
  93.                     break;
  94.             }
  95.         }
  96.  
  97.         private void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
  98.         {
  99.             ResetColumnWidths();
  100.         }
  101.  
  102.         private void ResetColumnWidths()
  103.         {
  104.             Dispatcher.CurrentDispatcher.BeginInvoke((Action) (() =>
  105.             {
  106.                 GridView view = listView.View as GridView;
  107.                 if (view != null)
  108.                 {
  109.                     listView.UpdateLayout();   // Prevent flickering on expanding columns (not collapsing though)
  110.                     foreach (GridViewColumn gridViewColumn in view.Columns)
  111.                     {
  112.                         bool? isFillColumn = RangeColumn.GetRangeIsFillColumn(gridViewColumn);
  113.                         if (!isFillColumn.HasValue || !isFillColumn.Value)
  114.                         {
  115.                             if (double.IsNaN(gridViewColumn.Width))
  116.                             {
  117.                                 gridViewColumn.Width = gridViewColumn.ActualWidth;
  118.                                 gridViewColumn.Width = double.NaN;
  119.                             }
  120.                         }
  121.                     }
  122.                 }
  123.                 Dispatcher.CurrentDispatcher.BeginInvoke((Action) Refresh, DispatcherPriority.Render);
  124.             }), DispatcherPriority.Render);
  125.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement