NullableBool Markup Extension

The NullableBool Markup Extension provides the ability to set nullable boolean dependency properties in XAML markup. These types of properties can normally be bound to, but can't be explicitly set to a specific value. This extension provides that capability.

Syntax

    <Page.Resources>
        <helpers:ObjectWithNullableBoolProperty x:Key="OurObject" NullableBool="{ex:NullableBool Value=True}"/>
    </Page.Resources>
    [Bindable]
    public class ObjectWithNullableBoolProperty : DependencyObject
    {
        public bool? NullableBool
        {
            get { return (bool?)GetValue(NullableBoolProperty); }
            set { SetValue(NullableBoolProperty, value); }
        }

        // Using a DependencyProperty as the backing store for NullableBool.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty NullableBoolProperty =
            DependencyProperty.Register(nameof(NullableBool), typeof(bool?), typeof(ObjectWithNullableBoolProperty), new PropertyMetadata(null));
    }

Properties

Property Type Description
IsNull bool When set to True will provide a null value and ignore the Value property. The default value is False.
Value bool Provides the specified True or False value when IsNull is set to False. The default value is False.

Requirements

Device family Universal, 10.0.16299.0 or higher
Namespace Microsoft.Toolkit.Uwp.UI.Extensions
NuGet package Microsoft.Toolkit.Uwp.UI

API Source Code