RectangleGeometry
沒有Width
屬性,至少在WPF中。
我的需要,我不得不創建一個IMultiValueConverter
如下所述:https://stackoverflow.com/a/5650367/2663813
在那之後,我仍然在角落的一個問題,所以我用肯特的第二個解決方案(注意Border.Fill
不存在任何)。
這裏是我寫的:
<Grid>
<Border x:Name="canvasBorder" CornerRadius="5">
<Border.Resources>
<tools:ContentClipConverter x:Key="ContentClipConverter" />
</Border.Resources>
<Border.Clip>
<MultiBinding Converter="{StaticResource ContentClipConverter}">
<Binding Path="ActualWidth"
RelativeSource="{RelativeSource Self}"/>
<Binding Path="ActualHeight"
RelativeSource="{RelativeSource Self}"/>
<Binding Path="CornerRadius"
RelativeSource="{RelativeSource Self}"/>
</MultiBinding>
</Border.Clip>
<!-- ... -->
</Border>
<Border BorderBrush="Black" BorderThickness="1" CornerRadius="5" Background="Transparent" IsHitTestVisible="false" />
</Grid>
和ContentClipConverter.cs內:
/// <summary>
/// Clips the content of a rounded corner border.
/// Code taken from <a href="https://stackoverflow.com/a/5650367/2663813">this topic</a>
/// </summary>
public class ContentClipConverter : IMultiValueConverter {
/// <summary>
/// Gets a clipping geometry for the item
/// </summary>
/// <param name="values">The input values</param>
/// <param name="targetType">The parameter is not used.</param>
/// <param name="parameter">The parameter is not used.</param>
/// <param name="culture">The parameter is not used.</param>
/// <returns>The clipping geometry</returns>
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) {
if (values.Length == 3 && values[0] is double && values[1] is double && values[2] is CornerRadius) {
var width = (double)values[0];
var height = (double)values[1];
if (width < double.Epsilon || height < double.Epsilon) {
return Geometry.Empty;
}
var radius = (CornerRadius)values[2];
// Actually we need more complex geometry, when CornerRadius has different values.
// But let me not to take this into account, and simplify example for a common value.
var clip = new RectangleGeometry(new Rect(0, 0, width, height), radius.TopLeft, radius.TopLeft);
clip.Freeze();
return clip;
}
return DependencyProperty.UnsetValue;
}
/// <summary>
/// Not implemented
/// </summary>
/// <param name="value">The parameter is not used.</param>
/// <param name="targetTypes">The parameter is not used.</param>
/// <param name="parameter">The parameter is not used.</param>
/// <param name="culture">The parameter is not used.</param>
/// <returns>This function does not return anything</returns>
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) {
throw new NotSupportedException();
}