Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClipToBounds property in WinRT

I'm trying to find the equivalent of ClipToBounds in Windows Runtime. If it doesn't exists is there a way to recreate this behavior ?

like image 715
Bahaïka Avatar asked Dec 02 '12 10:12

Bahaïka


3 Answers

Here is the solution I use :

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);
    }
}

Found it here

like image 107
Bahaïka Avatar answered Nov 03 '22 02:11

Bahaïka


I prefer the 'Clip' property here is some xaml

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

The parameter of the 'Rect' property are: Rect="x y width height"

Hope it helps

Greetings

like image 32
Jan L Avatar answered Nov 03 '22 01:11

Jan L


This is implemented in WinRTXamlToolkit (https://github.com/xyzzer/WinRTXamlToolkit) which is also available as Nuget package.

Add to XAML header:

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

Then, for example in XAML Canvas component

<Canvas extensions:FrameworkElementExtensions.ClipToBounds="True"/>
like image 43
Erkki Nokso-Koivisto Avatar answered Nov 03 '22 02:11

Erkki Nokso-Koivisto