Created
August 17, 2011 18:01
-
-
Save follesoe/1152173 to your computer and use it in GitHub Desktop.
ItemsControlVisibilityHelper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ItemsControlVisibilityHelper : ContentControl | |
{ | |
public static readonly DependencyProperty ItemsControlProperty = | |
DependencyProperty.Register("ItemsControl", | |
typeof (ItemsControl), | |
typeof (ItemsControlVisibilityHelper), | |
new PropertyMetadata(default(ItemsControl), OnItemsControlChanged)); | |
public ItemsControl ItemsControl | |
{ | |
get { return (ItemsControl) GetValue(ItemsControlProperty); } | |
set { SetValue(ItemsControlProperty, value); } | |
} | |
public static readonly DependencyProperty BoundItemProperty = | |
DependencyProperty.Register("BoundItem", | |
typeof (object), | |
typeof (ItemsControlVisibilityHelper), | |
new PropertyMetadata(default(object), OnBoundItemChanged)); | |
public object BoundItem | |
{ | |
get { return GetValue(BoundItemProperty); } | |
set { SetValue(BoundItemProperty, value); } | |
} | |
private static void OnItemsControlChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) | |
{ | |
var helper = obj as ItemsControlVisibilityHelper; | |
helper.UpdateVisibility(); | |
} | |
private static void OnBoundItemChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) | |
{ | |
var helper = obj as ItemsControlVisibilityHelper; | |
helper.UpdateVisibility(); | |
} | |
public void UpdateVisibility() | |
{ | |
if(ItemsControl != null && BoundItem != null) | |
{ | |
if(ItemsControl.ItemsSource is IList) | |
{ | |
var collection = ItemsControl.ItemsSource as IList; | |
var index = collection.IndexOf(BoundItem); | |
if(index == collection.Count - 1) | |
{ | |
Visibility = Visibility.Collapsed; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment