Guest User

Untitled

a guest
Jan 24th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. public class ItemsControlVisibilityHelper : ContentControl
  2. {
  3. public static readonly DependencyProperty ItemsControlProperty =
  4. DependencyProperty.Register("ItemsControl",
  5. typeof (ItemsControl),
  6. typeof (ItemsControlVisibilityHelper),
  7. new PropertyMetadata(default(ItemsControl), OnItemsControlChanged));
  8.  
  9. public ItemsControl ItemsControl
  10. {
  11. get { return (ItemsControl) GetValue(ItemsControlProperty); }
  12. set { SetValue(ItemsControlProperty, value); }
  13. }
  14.  
  15. public static readonly DependencyProperty BoundItemProperty =
  16. DependencyProperty.Register("BoundItem",
  17. typeof (object),
  18. typeof (ItemsControlVisibilityHelper),
  19. new PropertyMetadata(default(object), OnBoundItemChanged));
  20.  
  21. public object BoundItem
  22. {
  23. get { return GetValue(BoundItemProperty); }
  24. set { SetValue(BoundItemProperty, value); }
  25. }
  26.  
  27. private static void OnItemsControlChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
  28. {
  29. var helper = obj as ItemsControlVisibilityHelper;
  30. helper.UpdateVisibility();
  31. }
  32.  
  33. private static void OnBoundItemChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
  34. {
  35. var helper = obj as ItemsControlVisibilityHelper;
  36. helper.UpdateVisibility();
  37. }
  38.  
  39. public void UpdateVisibility()
  40. {
  41. if(ItemsControl != null && BoundItem != null)
  42. {
  43. if(ItemsControl.ItemsSource is IList)
  44. {
  45. var collection = ItemsControl.ItemsSource as IList;
  46. var index = collection.IndexOf(BoundItem);
  47.  
  48. if(index == collection.Count - 1)
  49. {
  50. Visibility = Visibility.Collapsed;
  51. }
  52. }
  53. }
  54. }
  55. }
Add Comment
Please, Sign In to add comment