Windows ランタイムでClipToBoundsに相当するものを見つけようとしています。存在しない場合、この動作を再現する方法はありますか?
2845 次
3 に答える
5
私が使用するソリューションは次のとおりです。
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);
}
}
ここで見つけた
于 2012-12-02T10:43:14.963 に答える
3
ここの「Clip」プロパティはいくつかのxamlです
<Grid Width="100" Height="50">
<Grid.Clip>
<RectangleGeometry Rect="0 0 100 50"/>
</Grid.Clip>
</Grid>
「Rect」プロパティのパラメータは次のとおりです: Rect="xy width height"
それが役に立てば幸い
ご挨拶
于 2015-03-27T08:40:31.707 に答える
2
これは、Nuget パッケージとしても利用できるWinRTXamlToolkit ( https://github.com/xyzzer/WinRTXamlToolkit ) に実装されています。
XAML ヘッダーに追加します。
xmlns:extensions="using:WinRTXamlToolkit.Controls.Extensions"
次に、たとえば XAML Canvas コンポーネントで
<Canvas extensions:FrameworkElementExtensions.ClipToBounds="True"/>
于 2016-03-08T12:50:21.847 に答える