In my last post, I lamented the loss of the Hidden in the Windows.UI.Xaml.Visibility enumeration. Here’s an ultra-simple attached property that simulates Hidden by changing the opacity and hit testability of the element:
using Windows.UI.Xaml;
namespace Blog.VisibilityHiddenDemo
{
public class VisibilityHiddenBehavior : DependencyObject
{
public static readonly DependencyProperty IsVisibleProperty =
DependencyProperty.RegisterAttached("IsVisible", typeof(bool), typeof(VisibilityHiddenBehavior),
new PropertyMetadata(true, OnIsVisibleChanged));
public static bool GetIsVisible(DependencyObject obj)
{
return (bool)obj.GetValue(IsVisibleProperty);
}
public static void SetIsVisible(DependencyObject obj, bool value)
{
obj.SetValue(IsVisibleProperty, value);
}
private static void OnIsVisibleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var element = d as FrameworkElement;
if (element != null)
{
var visible = (bool)e.NewValue;
element.Opacity = visible ? 1 : 0;
element.IsHitTestVisible = visible;
}
}
}
}
Free as in beer.
Happy coding.
No comments:
Post a Comment