Click here to Skip to main content
12,756,259 members (34,981 online)
Click here to Skip to main content
Add your own
alternative version


3 bookmarked
Posted 15 Oct 2012

VisibilityControl – The Design

, 15 Oct 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
In this post, I briefly discuss the design behind the VisibilityControl.

In this post, I briefly discuss the design behind the VisibilityControl.


Posts in this series:

If you look at the source code history at codeplex, you will notice that I first designed this control with two dependency properties, one for the true content and one for the false content. The resulting XAML that the user needed to enter to use the control seems a bit clunky to me. Therefore, I switched to an ItemsControl-derived solution. I ask the user of the control to load the Items property with the false and true content respectively.

It does seem a bit weird to use a list to hold two and only two items, but there you have it. Maybe someday I can shore-up the sanity of this choice by making the control be able to display one of a series of items based upon the value of an int, enum, or string.

The only real thing to notice about the source below is to understand that we set the value of the dependency property called VisibleContentto the appropriate user interface element found in the list, based upon the value of IsTrue. The default style simply shows the content of VisibleContent.

using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Markup;

namespace VisibilityControlLibrary
    [ContentProperty(Name = "Items")]
    public class VisibilityControl : ItemsControl
        public VisibilityControl()
            this.DefaultStyleKey = typeof(VisibilityControl);
            Items.VectorChanged += OnItemsChanged;

        public bool IsTrue
            get { return (bool)GetValue(IsTrueProperty); }
            set { SetValue(IsTrueProperty, value); }

        public static readonly DependencyProperty IsTrueProperty =
            DependencyProperty.Register("IsTrue", typeof(bool), 
            typeof(VisibilityControl), new PropertyMetadata(null, OnIsTrueChanged));

        public object VisibleContent
            get { return (object)GetValue(VisibleContentProperty); }
            private set { SetValue(VisibleContentProperty, value); }

        public static readonly DependencyProperty VisibleContentProperty =
            DependencyProperty.Register("VisibleContent", typeof(object), 
            typeof(VisibilityControl), new PropertyMetadata(null));

        private static void OnIsTrueChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
            var visibilityItemsControl = d as VisibilityControl;
            if (visibilityItemsControl != null)

        void OnItemsChanged(IObservableVector<object> sender, IVectorChangedEventArgs evt)

        void Evaluate()
            if (Items.Count >= 2)
                VisibleContent = Items[IsTrue ? 1 : 0];
                VisibleContent = null;
     <Setter Property="Template">
             <ControlTemplate TargetType="local:VisibilityControl">
                         Content="{TemplateBinding VisibleContent}" />


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

John Michael Hauck
Software Developer (Senior) LECO Corporation
United States United States
John Hauck has been developing software professionally since 1981, and focused on Windows-based development since 1988. For the past 17 years John has been working at LECO, a scientific laboratory instrument company, where he manages software development. John also served as the manager of software development at Zenith Data Systems, as the Vice President of software development at TechSmith, as the lead medical records developer at Instrument Makar, as the MSU student who developed the time and attendance system for Dart container, and as the high school kid who wrote the manufacturing control system at Wohlert. John loves the Lord, his wife, their three kids, and sailing on Lake Michigan.

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 15 Oct 2012
Article Copyright 2012 by John Michael Hauck
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid