我有一个包含路径的控制模板(除其他控件之外)。调整控制大小时,应调整路径大小。描述该路径的点和大小可以表示为控制尺寸的相关分数。

这是模板的摘录:

<Path Stroke="Gray" StrokeThickness="5">
    <Path.Data>
        <PathGeometry>
            <PathFigure StartPoint="{TemplateBinding Start}" >
                <ArcSegment Point="{TemplateBinding End}" Size="{TemplateBinding Size}" RotationAngle="0" IsLargeArc="True" />
            </PathFigure>
        </PathGeometry>
    </Path.Data>
</Path>
.

启动和结束是类型点的依赖位置,大小是类型大小的依赖项。

目前正在做的是侦听FrameworkElement.SizeChanged事件:

void OperationModeIndicator_SizeChanged( object sender, SizeChangedEventArgs e )
{
    this.Size = new Size( e.NewSize.Width * 0.45f, e.NewSize.Height * 0.45f );
    this.Start = new Point( e.NewSize.Width * 0.35f, e.NewSize.Height * 0.1f );
    this.End = new Point( e.NewSize.Width * 0.65f, e.NewSize.Height * 0.1f );
}
.

现在是问题: 是否有另一个(更优雅)的方式将路径的属性绑定到父控件的大小?

有帮助吗?

解决方案

你有什么可能是实现这一目标的最佳方式。

另一种方式是构建自定义 imultivalueConverter 暴露两个公共属性:Wixpercentenge和高度。然后,您可以绑定到模板父母的实际宽/实际亮度。

public class MyConverter : IMultiValueConverter {

    public double HeightPercentage { get; set; }
    public double WidthPercentage { get; set; }

    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) {
        // TODO: Validate values size and types

        return new Point(((double)values[0]) * this.WidthPercentage, ((double)values[1]) * this.HeightPercentage);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) {
        // No-op or throw
    }

}
.

您使用的是:

<local:MyConverter x:Key="StartPointConverter"
    WidthPercentage="0.35" HeightPercentage="0.1" />

<!-- ... -->

<PathFigure>
    <PathFigure.StartPoint>
        <MultiBinding Converter="{StaticResource StartPointConverter}">
            <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="ActualWidth" />
            <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="ActualHeight" />
        </MultiBinding>
    </PathFigure.StartPoint>
    <!-- ... -->
</PathFigure>
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top