我要绘制一个均匀适合其空间的圆,具有恒定的描边厚度。 ViewBox 让我得到统一的配合,但不是恒定的描边厚度。
<Viewbox Stretch="Uniform" MinHeight="10" MinWidth="10" >
<Ellipse Height="10" Width="10" Fill="Red" StrokeThickness="1" Stroke="Yellow"/>
</Viewbox>
最佳答案
如果您没有指定椭圆的宽度或高度,则默认值为“自动”。结合默认的 HorizontalAlignment/VerticalAligment 值“Stretch”,这应该会导致椭圆“拉伸(stretch)”到其容器的宽度和高度(具有恒定的笔画粗细)。
父容器的 *ContentAlignment 属性可能会影响此行为,但同样,默认的未设置值应该会为您提供所需的行为。
编辑:修改我的建议,因为我没有意识到椭圆必须保持圆形(别担心,我决定拿起一本“阅读理解”)。
我建议您将椭圆的宽度和高度属性绑定(bind)到父容器的 ActualWidth 和 ActualHeight 属性的 MultiBinding。然后实现一个“多值转换器”,它将返回多重绑定(bind)的最小值。
所以转换器可能看起来像这样:
class MinimumValueConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return values.Cast<double>().Min();
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
椭圆属性可以这样绑定(bind):
<Window.Resources>
<l:MinimumValueConverter x:Key="MinimumValueConverter" />
</Window.Resources>
<Ellipse Stroke="Black" StrokeThickness="1">
<Ellipse.Width>
<MultiBinding Converter="{StaticResource MinimumValueConverter}">
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type UIElement}}" Path="ActualWidth" />
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type UIElement}}" Path="ActualHeight" />
</MultiBinding>
</Ellipse.Width>
<Ellipse.Height>
<MultiBinding Converter="{StaticResource MinimumValueConverter}">
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type UIElement}}" Path="ActualWidth" />
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type UIElement}}" Path="ActualHeight" />
</MultiBinding>
</Ellipse.Height>
</Ellipse>
关于.net - 是否有 WPF XAML 用于统一拟合但恒定的 StrokeThickness,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/677903/