【问题标题】:ClipToBounds property in WinRTWinRT 中的 ClipToBounds 属性
【发布时间】:2012-12-02 10:02:28
【问题描述】:

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

【问题讨论】:

  • 这是一篇很棒的文章https://www.domysee.com/blogposts/canvas-rendering-out-of-bounds

标签: wpf windows-runtime


【解决方案1】:

这是我使用的解决方案:

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

【讨论】:

    【解决方案2】:

    我更喜欢这里的“剪辑”属性是一些 xaml

    <Grid Width="100" Height="50">
        <Grid.Clip>
            <RectangleGeometry Rect="0 0 100 50"/>
        </Grid.Clip>
    </Grid>
    

    'Rect'属性的参数为:Rect="x y width height"

    希望对你有帮助

    问候

    【讨论】:

      【解决方案3】:

      这是在 WinRTXamlToolkit (https://github.com/xyzzer/WinRTXamlToolkit) 中实现的,它也可以作为 Nuget 包使用。

      添加到 XAML 标头:

      xmlns:extensions="using:WinRTXamlToolkit.Controls.Extensions"
      

      然后,例如在 XAML Canvas 组件中

      <Canvas extensions:FrameworkElementExtensions.ClipToBounds="True"/>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-29
        相关资源
        最近更新 更多