-
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 9 commits
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
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
29 changes: 29 additions & 0 deletions
29
Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ClipToBounds/ClipToBoundsCode.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,29 @@ | ||
<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:UIElementExtensions.ClipToBounds="@[Clip to Bounds:Bool:True]"> | ||
<!-- We translate the inner rectangles outside of the bounds of the container. --> | ||
<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
Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ClipToBounds/ClipToBoundsPage.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.ClipToBoundsPage" | ||
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:UIElementExtensions.ClipToBounds="True"/> | ||
|
||
</Page> |
16 changes: 16 additions & 0 deletions
16
Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ClipToBounds/ClipToBoundsPage.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 ClipToBoundsPage : Page | ||
{ | ||
public ClipToBoundsPage() => 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
51 changes: 51 additions & 0 deletions
51
Microsoft.Toolkit.Uwp.UI/Extensions/UIElement/UIElementExtensions.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,51 @@ | ||
// 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 | ||
{ | ||
/// <summary> | ||
/// Provides attached dependency properties for the <see cref="Windows.UI.Xaml.UIElement"/> | ||
/// </summary> | ||
public static class UIElementExtensions | ||
{ | ||
/// <summary> | ||
/// Attached <see cref="DependencyProperty"/> that indicates whether or not the contents of the target <see cref="UIElement"/> should always be clipped to their parent's bounds. | ||
/// </summary> | ||
public static readonly DependencyProperty ClipToBoundsProperty = DependencyProperty.RegisterAttached( | ||
"ClipToBounds", | ||
typeof(bool), | ||
typeof(UIElementExtensions), | ||
new PropertyMetadata(DependencyProperty.UnsetValue, OnClipToBoundsPropertyChanged)); | ||
|
||
/// <summary> | ||
/// Gets the value of <see cref="ClipToBoundsProperty"/> | ||
/// </summary> | ||
/// <param name="element">The <see cref="UIElement"/> to read the property value from</param> | ||
/// <returns>The <see cref="bool"/> associated with the <see cref="FrameworkElement"/></returns>. | ||
public static bool GetClipToBounds(UIElement element) => (bool)element.GetValue(ClipToBoundsProperty); | ||
|
||
/// <summary> | ||
/// Sets the value of <see cref="ClipToBoundsProperty"/> | ||
/// </summary> | ||
/// <param name="element">The <see cref="UIElement"/> to set the property to</param> | ||
/// <param name="value">The new value of the attached property</param> | ||
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; | ||
} | ||
|
||
var clipToBounds = (bool)e.NewValue; | ||
var visual = ElementCompositionPreview.GetElementVisual(element); | ||
visual.Clip = clipToBounds ? visual.Compositor.CreateInsetClip() : null; | ||
vgromfeld marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} |
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.