我想画一个均匀地适合其空间的圆,具有恒定的笔划厚度。ViewBox给了我均匀的适合度,但不是恒定的笔划厚度。
<Viewbox Stretch="Uniform" MinHeight="10" MinWidth="10" >
    <Ellipse Height="10" Width="10" Fill="Red" StrokeThickness="1" Stroke="Yellow"/>
</Viewbox>发布于 2009-03-24 15:56:16
如果不指定椭圆的宽度或高度,则默认值为"Auto“。结合默认的HorizontalAlignment/VerticalAligment值"Stretch",这应该会导致椭圆“拉伸”到其容器的宽度和高度(具有恒定的笔触粗细)。
父容器的*ContentAlignment属性可能会影响此行为,但同样,默认的未设置值应该会为您提供所需的行为。
编辑:修改我的建议,因为我没有意识到椭圆必须保持为一个圆(别担心,我已经决定拿一本“为了理解而阅读”)。
我建议您将椭圆的宽度和高度属性绑定到父容器的ActualWidth和ActualHeight属性的MultiBinding。然后实现一个“多值转换器”,它将从多绑定中返回最小值。
因此,转换器可能如下所示:
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();
    }
}椭圆属性可以像这样绑定:
<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>https://stackoverflow.com/questions/677903
复制相似问题