From 71cc3e312c1611d89553f58f378900044d5c6e64 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 25 Jan 2020 16:25:39 +0100 Subject: [PATCH 1/5] 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 00000000000..d12c5d357b7 --- /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; + } + } +} From 586324ca62e7d4edd1204c3dc0bb0a6b806306d2 Mon Sep 17 00:00:00 2001 From: Vincent Date: Mon, 3 Feb 2020 04:40:16 -0800 Subject: [PATCH 2/5] add clip to bound framework element extension --- .../Microsoft.Toolkit.Uwp.SampleApp.csproj | 10 +++ ...workElementExtensionsClipToBoundsCode.bind | 28 ++++++++ ...workElementExtensionsClipToBoundsPage.xaml | 13 ++++ ...kElementExtensionsClipToBoundsPage.xaml.cs | 16 +++++ .../SamplePages/samples.json | 9 +++ ...FrameworkElementExtensions.ClipToBounds.cs | 70 +++++++++++++++++++ 6 files changed, 146 insertions(+) create mode 100644 Microsoft.Toolkit.Uwp.SampleApp/SamplePages/FrameworkElementExtensions.ClipToBounds/FrameworkElementExtensionsClipToBoundsCode.bind create mode 100644 Microsoft.Toolkit.Uwp.SampleApp/SamplePages/FrameworkElementExtensions.ClipToBounds/FrameworkElementExtensionsClipToBoundsPage.xaml create mode 100644 Microsoft.Toolkit.Uwp.SampleApp/SamplePages/FrameworkElementExtensions.ClipToBounds/FrameworkElementExtensionsClipToBoundsPage.xaml.cs create mode 100644 Microsoft.Toolkit.Uwp.UI/Extensions/FrameworkElement/FrameworkElementExtensions.ClipToBounds.cs diff --git a/Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj b/Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj index d95e24c902f..b1bd6910ff0 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj +++ b/Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj @@ -525,6 +525,9 @@ EyedropperPage.xaml + + FrameworkElementExtensionsClipToBoundsPage.xaml + ImageExLazyLoadingControl.xaml @@ -938,6 +941,13 @@ Designer MSBuild:Compile + + Designer + + + Designer + MSBuild:Compile + Designer MSBuild:Compile diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/FrameworkElementExtensions.ClipToBounds/FrameworkElementExtensionsClipToBoundsCode.bind b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/FrameworkElementExtensions.ClipToBounds/FrameworkElementExtensionsClipToBoundsCode.bind new file mode 100644 index 00000000000..cc9ac7ea6f8 --- /dev/null +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/FrameworkElementExtensions.ClipToBounds/FrameworkElementExtensionsClipToBoundsCode.bind @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/FrameworkElementExtensions.ClipToBounds/FrameworkElementExtensionsClipToBoundsPage.xaml b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/FrameworkElementExtensions.ClipToBounds/FrameworkElementExtensionsClipToBoundsPage.xaml new file mode 100644 index 00000000000..a365f3a60c1 --- /dev/null +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/FrameworkElementExtensions.ClipToBounds/FrameworkElementExtensionsClipToBoundsPage.xaml @@ -0,0 +1,13 @@ + + + + + diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/FrameworkElementExtensions.ClipToBounds/FrameworkElementExtensionsClipToBoundsPage.xaml.cs b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/FrameworkElementExtensions.ClipToBounds/FrameworkElementExtensionsClipToBoundsPage.xaml.cs new file mode 100644 index 00000000000..edc69aa82e9 --- /dev/null +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/FrameworkElementExtensions.ClipToBounds/FrameworkElementExtensionsClipToBoundsPage.xaml.cs @@ -0,0 +1,16 @@ +// 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.Controls; + +namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages +{ + /// + /// A page that shows how to use the ClipToBounds extension. + /// + public sealed partial class FrameworkElementExtensionsClipToBoundsPage : Page + { + public FrameworkElementExtensionsClipToBoundsPage() => InitializeComponent(); + } +} diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json index 22cef444cca..715c6a92b27 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json @@ -1069,6 +1069,15 @@ "Icon": "/Assets/Helpers.png", "DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/extensions/FrameworkElementExtensions.md" }, + { + "Name": "FrameworkElementExtensions.ClipToBounds", + "Type": "FrameworkElementExtensionsClipToBoundsPage", + "About": "Extension to clip the FrameworkElement inner controls inside its bounds.", + "CodeUrl": "https://github.com/windows-toolkit/WindowsCommunityToolkit/tree/master/Microsoft.Toolkit.Uwp.UI/Extensions/FrameworkElement", + "XamlCodeFile": "FrameworkElementExtensionsClipToBoundsCode.bind", + "Icon": "/Assets/Helpers.png", + "DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/extensions/FrameworkElementExtensions.md" + }, { "Name": "StringExtensions", "Type": "StringExtensionsPage", diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/FrameworkElement/FrameworkElementExtensions.ClipToBounds.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/FrameworkElement/FrameworkElementExtensions.ClipToBounds.cs new file mode 100644 index 00000000000..92a6cfa5e47 --- /dev/null +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/FrameworkElement/FrameworkElementExtensions.ClipToBounds.cs @@ -0,0 +1,70 @@ +// 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 System.Diagnostics; +using Windows.Foundation; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Media; + +namespace Microsoft.Toolkit.Uwp.UI.Extensions +{ + /// + /// Provides attached dependency properties for the + /// + public static partial class FrameworkElementExtensions + { + /// + /// Attached to activate clipping of a content inside the element bounds. + /// + /// + /// This property comes from WPF but is not supported natively in UWP apps. + /// See https://docs.microsoft.com/en-us/dotnet/api/system.windows.uielement.cliptobounds?view=netframework-4.8. + /// + public static readonly DependencyProperty ClipToBoundsProperty = DependencyProperty.RegisterAttached( + "ClipToBounds", + typeof(bool), + typeof(FrameworkElementExtensions), + new PropertyMetadata(false, OnClipToBoundsPropertyChanged)); + + /// + /// Get the value of . + /// + /// The object that will host the value. + /// The property value. . + public static bool GetClipToBounds(DependencyObject obj) => (bool)obj.GetValue(ClipToBoundsProperty); + + /// + /// Set the value of . + /// + /// The target object where to store the value. + /// The value to store. + public static void SetClipToBounds(DependencyObject obj, bool value) => obj.SetValue(ClipToBoundsProperty, value); + + private static void OnClipToBoundsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var element = d as FrameworkElement; + Debug.Assert(element != null, "Property should only be attached to FrameworkElement"); + + void OnSizeChanged(object sender, SizeChangedEventArgs sizeChangedEventArgs) => UpdateClipToBounds(element); + + var newValue = (bool)e.NewValue; + if (newValue) + { + element.SizeChanged += OnSizeChanged; + UpdateClipToBounds(element); + } + else + { + element.SizeChanged -= OnSizeChanged; + element.Clip = null; + } + } + + private static void UpdateClipToBounds(FrameworkElement element) + => element.Clip = new RectangleGeometry + { + Rect = new Rect(0, 0, element.ActualWidth, element.ActualHeight), + }; + } +} From 24163eaf83b44647089d9a51d364f7f4b10b9c8b Mon Sep 17 00:00:00 2001 From: Vincent Date: Wed, 25 Mar 2020 18:16:16 +0100 Subject: [PATCH 3/5] Add sample to sergio's pr --- .../Microsoft.Toolkit.Uwp.SampleApp.csproj | 8 +-- .../ClipToBoundsCode.bind} | 3 +- .../ClipToBoundsPage.xaml} | 4 +- .../ClipToBoundsPage.xaml.cs} | 4 +- .../SamplePages/samples.json | 8 +-- ...FrameworkElementExtensions.ClipToBounds.cs | 70 ------------------- .../UIElement/UIElementExtensions.cs | 23 +++--- 7 files changed, 23 insertions(+), 97 deletions(-) rename Microsoft.Toolkit.Uwp.SampleApp/SamplePages/{FrameworkElementExtensions.ClipToBounds/FrameworkElementExtensionsClipToBoundsCode.bind => ClipToBounds/ClipToBoundsCode.bind} (84%) rename Microsoft.Toolkit.Uwp.SampleApp/SamplePages/{FrameworkElementExtensions.ClipToBounds/FrameworkElementExtensionsClipToBoundsPage.xaml => ClipToBounds/ClipToBoundsPage.xaml} (70%) rename Microsoft.Toolkit.Uwp.SampleApp/SamplePages/{FrameworkElementExtensions.ClipToBounds/FrameworkElementExtensionsClipToBoundsPage.xaml.cs => ClipToBounds/ClipToBoundsPage.xaml.cs} (71%) delete mode 100644 Microsoft.Toolkit.Uwp.UI/Extensions/FrameworkElement/FrameworkElementExtensions.ClipToBounds.cs diff --git a/Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj b/Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj index b1bd6910ff0..f3db30d6f4e 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj +++ b/Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj @@ -525,8 +525,8 @@ EyedropperPage.xaml - - FrameworkElementExtensionsClipToBoundsPage.xaml + + ClipToBoundsPage.xaml ImageExLazyLoadingControl.xaml @@ -941,10 +941,10 @@ Designer MSBuild:Compile - + Designer - + Designer MSBuild:Compile diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/FrameworkElementExtensions.ClipToBounds/FrameworkElementExtensionsClipToBoundsCode.bind b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ClipToBounds/ClipToBoundsCode.bind similarity index 84% rename from Microsoft.Toolkit.Uwp.SampleApp/SamplePages/FrameworkElementExtensions.ClipToBounds/FrameworkElementExtensionsClipToBoundsCode.bind rename to Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ClipToBounds/ClipToBoundsCode.bind index cc9ac7ea6f8..590a8f82be5 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/FrameworkElementExtensions.ClipToBounds/FrameworkElementExtensionsClipToBoundsCode.bind +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ClipToBounds/ClipToBoundsCode.bind @@ -12,7 +12,8 @@ BorderThickness="1" Width="148" Height="148" - extensions:FrameworkElementExtensions.ClipToBounds="@[Clip to Bounds:Bool:True]"> + extensions:UIElementExtensions.ClipToBounds="@[Clip to Bounds:Bool:True]"> + diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/FrameworkElementExtensions.ClipToBounds/FrameworkElementExtensionsClipToBoundsPage.xaml b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ClipToBounds/ClipToBoundsPage.xaml similarity index 70% rename from Microsoft.Toolkit.Uwp.SampleApp/SamplePages/FrameworkElementExtensions.ClipToBounds/FrameworkElementExtensionsClipToBoundsPage.xaml rename to Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ClipToBounds/ClipToBoundsPage.xaml index a365f3a60c1..9e1fc882026 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/FrameworkElementExtensions.ClipToBounds/FrameworkElementExtensionsClipToBoundsPage.xaml +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ClipToBounds/ClipToBoundsPage.xaml @@ -1,5 +1,5 @@  + extensions:UIElementExtensions.ClipToBounds="True"/> diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/FrameworkElementExtensions.ClipToBounds/FrameworkElementExtensionsClipToBoundsPage.xaml.cs b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ClipToBounds/ClipToBoundsPage.xaml.cs similarity index 71% rename from Microsoft.Toolkit.Uwp.SampleApp/SamplePages/FrameworkElementExtensions.ClipToBounds/FrameworkElementExtensionsClipToBoundsPage.xaml.cs rename to Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ClipToBounds/ClipToBoundsPage.xaml.cs index edc69aa82e9..12c292a52a7 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/FrameworkElementExtensions.ClipToBounds/FrameworkElementExtensionsClipToBoundsPage.xaml.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ClipToBounds/ClipToBoundsPage.xaml.cs @@ -9,8 +9,8 @@ namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages /// /// A page that shows how to use the ClipToBounds extension. /// - public sealed partial class FrameworkElementExtensionsClipToBoundsPage : Page + public sealed partial class ClipToBoundsPage : Page { - public FrameworkElementExtensionsClipToBoundsPage() => InitializeComponent(); + public ClipToBoundsPage() => InitializeComponent(); } } diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json index 715c6a92b27..6c322da3dc4 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json @@ -1070,13 +1070,13 @@ "DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/extensions/FrameworkElementExtensions.md" }, { - "Name": "FrameworkElementExtensions.ClipToBounds", - "Type": "FrameworkElementExtensionsClipToBoundsPage", + "Name": "ClipToBounds", + "Type": "ClipToBoundsPage", "About": "Extension to clip the FrameworkElement inner controls inside its bounds.", "CodeUrl": "https://github.com/windows-toolkit/WindowsCommunityToolkit/tree/master/Microsoft.Toolkit.Uwp.UI/Extensions/FrameworkElement", - "XamlCodeFile": "FrameworkElementExtensionsClipToBoundsCode.bind", + "XamlCodeFile": "ClipToBoundsCode.bind", "Icon": "/Assets/Helpers.png", - "DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/extensions/FrameworkElementExtensions.md" + "DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/extensions/UIElementExtensions.md" }, { "Name": "StringExtensions", diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/FrameworkElement/FrameworkElementExtensions.ClipToBounds.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/FrameworkElement/FrameworkElementExtensions.ClipToBounds.cs deleted file mode 100644 index 92a6cfa5e47..00000000000 --- a/Microsoft.Toolkit.Uwp.UI/Extensions/FrameworkElement/FrameworkElementExtensions.ClipToBounds.cs +++ /dev/null @@ -1,70 +0,0 @@ -// 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 System.Diagnostics; -using Windows.Foundation; -using Windows.UI.Xaml; -using Windows.UI.Xaml.Media; - -namespace Microsoft.Toolkit.Uwp.UI.Extensions -{ - /// - /// Provides attached dependency properties for the - /// - public static partial class FrameworkElementExtensions - { - /// - /// Attached to activate clipping of a content inside the element bounds. - /// - /// - /// This property comes from WPF but is not supported natively in UWP apps. - /// See https://docs.microsoft.com/en-us/dotnet/api/system.windows.uielement.cliptobounds?view=netframework-4.8. - /// - public static readonly DependencyProperty ClipToBoundsProperty = DependencyProperty.RegisterAttached( - "ClipToBounds", - typeof(bool), - typeof(FrameworkElementExtensions), - new PropertyMetadata(false, OnClipToBoundsPropertyChanged)); - - /// - /// Get the value of . - /// - /// The object that will host the value. - /// The property value. . - public static bool GetClipToBounds(DependencyObject obj) => (bool)obj.GetValue(ClipToBoundsProperty); - - /// - /// Set the value of . - /// - /// The target object where to store the value. - /// The value to store. - public static void SetClipToBounds(DependencyObject obj, bool value) => obj.SetValue(ClipToBoundsProperty, value); - - private static void OnClipToBoundsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) - { - var element = d as FrameworkElement; - Debug.Assert(element != null, "Property should only be attached to FrameworkElement"); - - void OnSizeChanged(object sender, SizeChangedEventArgs sizeChangedEventArgs) => UpdateClipToBounds(element); - - var newValue = (bool)e.NewValue; - if (newValue) - { - element.SizeChanged += OnSizeChanged; - UpdateClipToBounds(element); - } - else - { - element.SizeChanged -= OnSizeChanged; - element.Clip = null; - } - } - - private static void UpdateClipToBounds(FrameworkElement element) - => element.Clip = new RectangleGeometry - { - Rect = new Rect(0, 0, element.ActualWidth, element.ActualHeight), - }; - } -} diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/UIElement/UIElementExtensions.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/UIElement/UIElementExtensions.cs index d12c5d357b7..932ea8e9400 100644 --- a/Microsoft.Toolkit.Uwp.UI/Extensions/UIElement/UIElementExtensions.cs +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/UIElement/UIElementExtensions.cs @@ -15,42 +15,37 @@ 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)); + 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); - } + public static bool GetClipToBounds(UIElement element) => (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); - } + 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 clipToBounds = (bool)e.NewValue; var visual = ElementCompositionPreview.GetElementVisual(element); - - visual.Clip = value ? visual.Compositor.CreateInsetClip() : null; + visual.Clip = clipToBounds ? visual.Compositor.CreateInsetClip() : null; } } } From a76393c459b040fb7fce1b0c7bcf2b7897102475 Mon Sep 17 00:00:00 2001 From: Vincent Date: Wed, 25 Mar 2020 18:22:52 +0100 Subject: [PATCH 4/5] fixed about --- Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json index 6c322da3dc4..02ef0c788d4 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json @@ -1072,7 +1072,7 @@ { "Name": "ClipToBounds", "Type": "ClipToBoundsPage", - "About": "Extension to clip the FrameworkElement inner controls inside its bounds.", + "About": "Extension to clip the UIElement inner controls inside its bounds.", "CodeUrl": "https://github.com/windows-toolkit/WindowsCommunityToolkit/tree/master/Microsoft.Toolkit.Uwp.UI/Extensions/FrameworkElement", "XamlCodeFile": "ClipToBoundsCode.bind", "Icon": "/Assets/Helpers.png", From 1a321b1c151041c391da312c80e151c0ae5ba714 Mon Sep 17 00:00:00 2001 From: Ze Groumf Date: Wed, 1 Apr 2020 22:01:50 +0200 Subject: [PATCH 5/5] address PR comments --- .../SamplePages/samples.json | 2 +- .../Extensions/UIElement/UIElementExtensions.cs | 11 ++++------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json index 02ef0c788d4..e7f77241fb6 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json @@ -1073,7 +1073,7 @@ "Name": "ClipToBounds", "Type": "ClipToBoundsPage", "About": "Extension to clip the UIElement inner controls inside its bounds.", - "CodeUrl": "https://github.com/windows-toolkit/WindowsCommunityToolkit/tree/master/Microsoft.Toolkit.Uwp.UI/Extensions/FrameworkElement", + "CodeUrl": "https://github.com/windows-toolkit/WindowsCommunityToolkit/tree/master/Microsoft.Toolkit.Uwp.UI/Extensions/UIElement", "XamlCodeFile": "ClipToBoundsCode.bind", "Icon": "/Assets/Helpers.png", "DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/extensions/UIElementExtensions.md" diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/UIElement/UIElementExtensions.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/UIElement/UIElementExtensions.cs index 932ea8e9400..fb340484aa6 100644 --- a/Microsoft.Toolkit.Uwp.UI/Extensions/UIElement/UIElementExtensions.cs +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/UIElement/UIElementExtensions.cs @@ -37,15 +37,12 @@ public static class UIElementExtensions private static void OnClipToBoundsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { - var element = d as UIElement; - if (element is null) + if (d is UIElement element) { - return; + var clipToBounds = (bool)e.NewValue; + var visual = ElementCompositionPreview.GetElementVisual(element); + visual.Clip = clipToBounds ? visual.Compositor.CreateInsetClip() : null; } - - var clipToBounds = (bool)e.NewValue; - var visual = ElementCompositionPreview.GetElementVisual(element); - visual.Clip = clipToBounds ? visual.Compositor.CreateInsetClip() : null; } } }