首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WinRT中的ClipToBounds属性

WinRT中的ClipToBounds属性
EN

Stack Overflow用户
提问于 2012-12-02 18:02:28
回答 3查看 2.8K关注 0票数 7

我正在尝试在Windows Runtime中找到ClipToBounds的等价物。如果它不存在,有没有办法重新创建这种行为?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-12-02 18:43:15

下面是我使用的解决方案:

代码语言:javascript
复制
public class Clip
{
    public static bool GetToBounds(DependencyObject depObj)
    {
        return (bool)depObj.GetValue(ToBoundsProperty);
    }

    public static void SetToBounds(DependencyObject depObj, bool clipToBounds)
    {
        depObj.SetValue(ToBoundsProperty, clipToBounds);
    }

    /// <summary>
    /// Identifies the ToBounds Dependency Property.
    /// <summary>
    public static readonly DependencyProperty ToBoundsProperty =
        DependencyProperty.RegisterAttached("ToBounds", typeof(bool),
        typeof(Clip), new PropertyMetadata(false, OnToBoundsPropertyChanged));

    private static void OnToBoundsPropertyChanged(DependencyObject d,
        DependencyPropertyChangedEventArgs e)
    {
        FrameworkElement fe = d as FrameworkElement;
        if (fe != null)
        {
            ClipToBounds(fe);

            // whenever the element which this property is attached to is loaded
            // or re-sizes, we need to update its clipping geometry
            fe.Loaded += new RoutedEventHandler(fe_Loaded);
            fe.SizeChanged += new SizeChangedEventHandler(fe_SizeChanged);
        }
    }

    /// <summary>
    /// Creates a rectangular clipping geometry which matches the geometry of the
    /// passed element
    /// </summary>
    private static void ClipToBounds(FrameworkElement fe)
    {
        if (GetToBounds(fe))
        {
            fe.Clip = new RectangleGeometry()
            {
                Rect = new Rect(0, 0, fe.ActualWidth, fe.ActualHeight)
            };
        }
        else
        {
            fe.Clip = null;
        }
    }

    static void fe_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        ClipToBounds(sender as FrameworkElement);
    }

    static void fe_Loaded(object sender, RoutedEventArgs e)
    {
        ClipToBounds(sender as FrameworkElement);
    }
}

找到了here

票数 5
EN

Stack Overflow用户

发布于 2015-03-27 16:40:32

我更喜欢这里的'Clip‘属性是一些xaml

代码语言:javascript
复制
<Grid Width="100" Height="50">
    <Grid.Clip>
        <RectangleGeometry Rect="0 0 100 50"/>
    </Grid.Clip>
</Grid>

'Rect‘属性的参数是: Rect="x y width height“

希望能有所帮助

问候

票数 3
EN

Stack Overflow用户

发布于 2016-03-08 20:50:22

这是在WinRTXamlToolkit (https://github.com/xyzzer/WinRTXamlToolkit)中实现的,它也可以通过Nuget包获得。

添加到XAML标头:

代码语言:javascript
复制
xmlns:extensions="using:WinRTXamlToolkit.Controls.Extensions"

然后,例如在XAML画布组件中

代码语言:javascript
复制
<Canvas extensions:FrameworkElementExtensions.ClipToBounds="True"/>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13668236

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档