gpt4 book ai didi

wpf - 如何在具有圆角半径的边框内设置控件样式

转载 作者:行者123 更新时间:2023-12-04 12:13:44 29 4
gpt4 key购买 nike

我有一个简单的窗口,其中包含一个带角半径的外边框和一个带背景的内边框。边框基本上只是我想放置在圆角外边框内的任何类型内容的占位符。

<Window x:Class="TestRunner.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" AllowsTransparency="True"
WindowStyle="None" Background="{x:Null}" >
<Border BorderThickness="2" BorderBrush="Black" CornerRadius="8" >
<Border Background="White">

</Border>
</Border>
</Window>

问题是内部控件不继承圆角,所以它在圆角的顶部绘制,如下所示:

Bad corner rendering

如何调整我的外部控件,以便内部控件不要尝试在圆角上绘制。

在内部控件上设置圆角不是一个可行的选择,因为它会导致角半径的可怕重复。

最佳答案

RectangleGeometry没有 Width属性,至少在 WPF 中。

为了我的需要,我不得不创建一个 IMultiValueConverter如此处所述:https://stackoverflow.com/a/5650367/2663813

在那之后,我在拐角处仍然有问题,所以我使用了 Kent 的第二个解决方案(注意 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();
}

关于wpf - 如何在具有圆角半径的边框内设置控件样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5448746/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com