通过模板绑定更改路径属性
我有一个包含路径的控件模板(除了其他控件之外)。调整控件大小时,应调整路径的大小。描述路径的点和大小可以表示为控制大小的相对分数。
以下是模板的摘录:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您所拥有的可能是实现这一目标的最佳方法。
另一种方法是构建自定义 IMultiValueConverter它公开两个公共属性:WidthPercentage 和 HeightPercentage。然后您可以绑定到模板化父项的 ActualWidth/ActualHeight。
你会这样使用:
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.
Which you'd use like: