From 71cc3e312c1611d89553f58f378900044d5c6e64 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 25 Jan 2020 16:25:39 +0100 Subject: [PATCH] Added UIElement.ClipToBounds property --- .../UIElement/UIElementExtensions.cs | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 Microsoft.Toolkit.Uwp.UI/Extensions/UIElement/UIElementExtensions.cs diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/UIElement/UIElementExtensions.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/UIElement/UIElementExtensions.cs new file mode 100644 index 000000000..d12c5d357 --- /dev/null +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/UIElement/UIElementExtensions.cs @@ -0,0 +1,56 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Windows.UI.Xaml; +using Windows.UI.Xaml.Hosting; + +namespace Microsoft.Toolkit.Uwp.UI.Extensions +{ + /// + /// Provides attached dependency properties for the + /// + public static class UIElementExtensions + { + /// + /// Attached that indicates whether or not the contents of the target should always be clipped to their parent's bounds. + /// + public static readonly DependencyProperty ClipToBoundsProperty = DependencyProperty.RegisterAttached("ClipToBounds", typeof(bool), typeof(UIElementExtensions), new PropertyMetadata(DependencyProperty.UnsetValue, OnClipToBoundsPropertyChanged)); + + /// + /// Gets the value of + /// + /// The to read the property value from + /// The associated with the . + public static bool GetClipToBounds(UIElement element) + { + return (bool)element.GetValue(ClipToBoundsProperty); + } + + /// + /// Sets the value of + /// + /// The to set the property to + /// The new value of the attached property + public static void SetClipToBounds(UIElement element, bool value) + { + element.SetValue(ClipToBoundsProperty, value); + } + + private static void OnClipToBoundsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var element = d as UIElement; + + if (element is null) + { + return; + } + + bool value = (bool)e.NewValue; + + var visual = ElementCompositionPreview.GetElementVisual(element); + + visual.Clip = value ? visual.Compositor.CreateInsetClip() : null; + } + } +}