Skip to content

Instantly share code, notes, and snippets.

@follesoe
Created August 17, 2011 18:01
Show Gist options
  • Save follesoe/1152173 to your computer and use it in GitHub Desktop.
Save follesoe/1152173 to your computer and use it in GitHub Desktop.
ItemsControlVisibilityHelper
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