wpf - WinRT 中的 ClipToBounds 属性

标签 wpf windows-runtime

我正在尝试查找 ClipToBounds 的等效项在 Windows 运行时中。 如果它不存在,是否有办法重新创建此行为?

最佳答案

这是我使用的解决方案:

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

关于wpf - WinRT 中的 ClipToBounds 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13668236/

相关文章:

c# - 如何将焦点放在 OnNavigatedTo 事件中的 TextBox 上?

c# - WCF 服务传递对象缓慢。那是正常的吗?

c# - WPF/Windows 8 应用程序 DataTemplate 数据绑定(bind)事件

wpf - WPF TextCompositionManager事件的帮助

c# - 同时写入两个文本框

windows-8 - 是否可以从 Windows 8 Metro Style 应用程序生成子进程?

c# - 在 C# 中将字符串转换为 IInputStream

c# - Win RT SQLite 事务性插入

c# - 将 WPF 应用程序连接到 MySQL 数据库

.net - 为什么 WPF Dispatcher.Invoke 不传播异常?