加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

wpf – WinRT中的ClipToBounds属性

发布时间:2020-12-13 20:39:24 所属栏目:Windows 来源:网络整理
导读:我试图在Windows运行时中找到相当于 ClipToBounds的内容. 如果它不存在有没有办法重新创建这种行为? 这是我使用的解决方案: public class Clip{ public static bool GetToBounds(DependencyObject depObj) { return (bool)depObj.GetValue(ToBoundsPropert
我试图在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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读