Is there a WPF XAML for a uniform fit but a constant StrokeThickness

I need to draw a circle that fits evenly into its space with a constant drain thickness. ViewBox gives me a uniform fit, but not a constant thickness of the drains.

<Viewbox Stretch="Uniform" MinHeight="10" MinWidth="10" >
    <Ellipse Height="10" Width="10" Fill="Red" StrokeThickness="1" Stroke="Yellow"/>
</Viewbox>
+3
source share
1 answer

If you do not specify the width or height of the ellipse, the default values ​​will be "Auto". Combined with the default Stretch HorizontalAlignment / VerticalAligment, this should cause the ellipse to “stretch” across the width and height of its container (with a constant thickness thickness).

* ContentAlignment , , unset , .

: , , ( , " " ).

MultiBinding ActualWidth ActualHeight. " ", .

, :

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>
+4

Source: https://habr.com/ru/post/1705266/


All Articles