.net - 是否有 WPF XAML 用于统一拟合但恒定的 StrokeThickness

标签 .net wpf xaml

我要绘制一个均匀适合其空间的圆,具有恒定的描边厚度。 ViewBox 让我得到统一的配合,但不是恒定的描边厚度。

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

最佳答案

如果您没有指定椭圆的宽度或高度,则默认值为“自动”。结合默认的 Horizo​​ntalAlignment/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/

相关文章:

.net - IronRuby ScriptSource.Execute 线程安全吗?

wpf - 如何在 XAML 中将颜色转换为画笔?

c# - 从不同线程读取和写入相同的内存

c# - 这里继承的可能意图是什么

c# - 在ViewModel(MVVM)内部使用 View 方法

wpf - 如何对同一元素应用多种效果

c# - DataGrid 的 CellEditingTemplate 和编辑模式下的焦点

c# - View 模型或XAML代码隐藏的WPF CollectionViewSource

c# - 添加 Microsoft.AspNetCore 程序包后无法解析 System.Runtime.InteropServices.RuntimeInformation

wpf - 显示选定的日历日期