通过模板绑定更改路径属性

发布于 2024-11-08 22:40:59 字数 1050 浏览 6 评论 0原文

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

以下是模板的摘录:

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

Start 和 End 是 Point 类型的 DependencyProperty,Size 是 Size 类型的 DependencyProperty。

我目前正在做的是监听 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 );
}

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

I have a control template that contains a path (besides other controls). The path should be resized when the control is resized. The points and the size that describe the path can be expressed as relativ fractions of the control size.

Here is an excerpt of the template:

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

Start and End are DependencyProperties of type Point, Size is a DependencyProperty of Type Size.

What I'm currently doing is listening to the FrameworkElement.SizeChanged event:

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 );
}

The question is now:
Is there another (more elegant) way to bind the path's properties to the size of the parent control?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

败给现实 2024-11-15 22:40:59

您所拥有的可能是实现这一目标的最佳方法。

另一种方法是构建自定义 IMultiValueConverter它公开两个公共属性:WidthPercentage 和 HeightPercentage。然后您可以绑定到模板化父项的 ActualWidth/ActualHeight。

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>

What you have is probably the best way to accomplish this.

Another way would be to build a custom IMultiValueConverter that exposes two public properties: WidthPercentage and HeightPercentage. Then you could bind to the ActualWidth/ActualHeight of the templated parent.

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
    }

}

Which you'd use like:

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文