wpf – WinRT中的ClipToBounds属性

前端之家收集整理的这篇文章主要介绍了wpf – WinRT中的ClipToBounds属性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在Windows运行时中找到相当于 ClipToBounds内容.
如果它不存在有没有办法重新创建这种行为?
这是我使用的解决方案:
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,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

原文链接:https://www.f2er.com/windows/372023.html

猜你在找的Windows相关文章