-
Notifications
You must be signed in to change notification settings - Fork 1.4k
UIElement.ClipToBounds property #3193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
michael-hawker
merged 12 commits into
CommunityToolkit:master
from
vgromfeld:clipToBounds
Apr 1, 2020
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
71cc3e3
Added UIElement.ClipToBounds property
Sergio0694 5cd7d9f
Merge branch 'master' into feature/clip-to-bounds
Sergio0694 4df9866
Merge branch 'master' into feature/clip-to-bounds
Sergio0694 586324c
add clip to bound framework element extension
vgromfeld de65c1e
Merge pull request #12 from Sergio0694/feature/clip-to-bounds
vgromfeld 24163ea
Add sample to sergio's pr
vgromfeld a76393c
fixed about
vgromfeld d8e65aa
Merge branch 'master' into clipToBounds
vgromfeld c98fbd5
Merge branch 'master' into clipToBounds
michael-hawker 1a321b1
address PR comments
vgromfeld f48d321
Merge branch 'clipToBounds' of https://github.com/vgromfeld/WindowsCo…
vgromfeld ce5f40d
Merge branch 'master' into clipToBounds
vgromfeld File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add clip to bound framework element extension
- Loading branch information
commit 586324ca62e7d4edd1204c3dc0bb0a6b806306d2
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...s/FrameworkElementExtensions.ClipToBounds/FrameworkElementExtensionsClipToBoundsCode.bind
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <Page | ||
| xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
| xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
| xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
| xmlns:extensions="using:Microsoft.Toolkit.Uwp.UI.Extensions" | ||
| xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
| mc:Ignorable="d"> | ||
|
|
||
| <Grid Padding="120"> | ||
| <Grid | ||
| BorderBrush="White" | ||
| BorderThickness="1" | ||
| Width="148" | ||
| Height="148" | ||
| extensions:FrameworkElementExtensions.ClipToBounds="@[Clip to Bounds:Bool:True]"> | ||
| <Rectangle Fill="Blue" Width="100" Height="100"> | ||
| <Rectangle.RenderTransform> | ||
| <TranslateTransform X="-50" Y="-50" /> | ||
| </Rectangle.RenderTransform> | ||
| </Rectangle> | ||
| <Rectangle Fill="Green" Width="100" Height="100"> | ||
| <Rectangle.RenderTransform> | ||
| <TranslateTransform X="50" Y="50" /> | ||
| </Rectangle.RenderTransform> | ||
| </Rectangle> | ||
| </Grid> | ||
| </Grid> | ||
| </Page> |
13 changes: 13 additions & 0 deletions
13
...s/FrameworkElementExtensions.ClipToBounds/FrameworkElementExtensionsClipToBoundsPage.xaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <Page | ||
| x:Class="Microsoft.Toolkit.Uwp.SampleApp.SamplePages.FrameworkElementExtensionsClipToBoundsPage" | ||
| xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
| xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
| xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
| xmlns:extensions="using:Microsoft.Toolkit.Uwp.UI.Extensions" | ||
| xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
| mc:Ignorable="d"> | ||
|
|
||
| <Grid x:Name="TargetObject" | ||
| extensions:FrameworkElementExtensions.ClipToBounds="True"/> | ||
|
|
||
| </Page> |
16 changes: 16 additions & 0 deletions
16
...rameworkElementExtensions.ClipToBounds/FrameworkElementExtensionsClipToBoundsPage.xaml.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| { | ||
| /// <summary> | ||
| /// A page that shows how to use the ClipToBounds extension. | ||
| /// </summary> | ||
| public sealed partial class FrameworkElementExtensionsClipToBoundsPage : Page | ||
| { | ||
| public FrameworkElementExtensionsClipToBoundsPage() => InitializeComponent(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...oft.Toolkit.Uwp.UI/Extensions/FrameworkElement/FrameworkElementExtensions.ClipToBounds.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| { | ||
| /// <summary> | ||
| /// Provides attached dependency properties for the <see cref="Windows.UI.Xaml.FrameworkElement"/> | ||
| /// </summary> | ||
| public static partial class FrameworkElementExtensions | ||
| { | ||
| /// <summary> | ||
| /// Attached <see cref="DependencyProperty"/> to activate clipping of a <see cref="FrameworkElement"/> content inside the element bounds. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// 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. | ||
| /// </remarks> | ||
| public static readonly DependencyProperty ClipToBoundsProperty = DependencyProperty.RegisterAttached( | ||
| "ClipToBounds", | ||
| typeof(bool), | ||
| typeof(FrameworkElementExtensions), | ||
| new PropertyMetadata(false, OnClipToBoundsPropertyChanged)); | ||
|
|
||
| /// <summary> | ||
| /// Get the value of <see cref="ClipToBoundsProperty"/>. | ||
| /// </summary> | ||
| /// <param name="obj">The object that will host the value.</param> | ||
| /// <returns>The property value. <see cref="ClipToBoundsProperty"/>.</returns> | ||
| public static bool GetClipToBounds(DependencyObject obj) => (bool)obj.GetValue(ClipToBoundsProperty); | ||
|
|
||
| /// <summary> | ||
| /// Set the value of <see cref="ClipToBoundsProperty"/>. | ||
| /// </summary> | ||
| /// <param name="obj">The target object where to store the value.</param> | ||
| /// <param name="value">The value to store.</param> | ||
| 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), | ||
| }; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.