Merge pull request #12 from Sergio0694/feature/clip-to-bounds

Merge Sergio0694 code in my own branch
This commit is contained in:
Vincent 2020-03-25 17:50:39 +01:00 коммит произвёл GitHub
Родитель 586324ca62 4df9866024
Коммит de65c1e511
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 56 добавлений и 0 удалений

Просмотреть файл

@ -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
{
/// <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)
{
return (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;
}
bool value = (bool)e.NewValue;
var visual = ElementCompositionPreview.GetElementVisual(element);
visual.Clip = value ? visual.Compositor.CreateInsetClip() : null;
}
}
}