Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 是否有WPF XAML用于均匀贴合但恒定的冲程厚度_.net_Wpf_Xaml - Fatal编程技术网

.net 是否有WPF XAML用于均匀贴合但恒定的冲程厚度

.net 是否有WPF XAML用于均匀贴合但恒定的冲程厚度,.net,wpf,xaml,.net,Wpf,Xaml,我想画一个圆,它均匀地适合它的空间,具有恒定的斯托克厚度。一个视图框可以使我获得均匀的拟合,但不能获得恒定的斯托克厚度 <Viewbox Stretch="Uniform" MinHeight="10" MinWidth="10" > <Ellipse Height="10" Width="10" Fill="Red" StrokeThickness="1" Stroke="Yellow"/> </Viewbox> 如果未指定椭圆的宽度或高度,默认值

我想画一个圆,它均匀地适合它的空间,具有恒定的斯托克厚度。一个视图框可以使我获得均匀的拟合,但不能获得恒定的斯托克厚度

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

如果未指定椭圆的宽度或高度,默认值将为“自动”。结合默认的水平对齐/垂直照明值“拉伸”,这将导致椭圆“拉伸”到其容器的宽度和高度(具有恒定的笔划厚度)

父容器的*ContentAlignment属性可能会影响此行为,但默认的未设置值也会提供所需的行为

编辑:修改我的建议,因为我没有意识到椭圆必须保持为一个圆(别担心,我决定拿起一本“阅读理解”)

我建议您将椭圆的宽度和高度属性绑定到父容器的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>
class MinimumValueConverter:IMultiValueConverter
{
公共对象转换(对象[]值,类型targetType,对象参数,System.Globalization.CultureInfo区域性)
{
返回值.Cast().Min();
}
公共对象[]转换回(对象值,类型[]目标类型,对象参数,System.Globalization.CultureInfo区域性)
{
抛出新的NotImplementedException();
}
}
椭圆属性可以这样绑定:

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>

但它不再是一个圆。:)