绑定到 ControlTemplate 中的转换
我正在尝试在 Silverlight 中创建一个自定义控件,该控件可以动态缩放其 ControlTemplate 中的元素。 ControlTemplate 的第一次尝试看起来像这样:
<ControlTemplate TargetType="controls:ProgressBar">
<Grid>
<Rectangle x:Name="TrackPart" Fill="{TemplateBinding Background}" HorizontalAlignment="Left" />
<Rectangle x:Name="ProgressPart" Fill="Blue" >
<Rectangle.RenderTransform>
<ScaleTransform ScaleX="{TemplateBinding Progress}" />
</Rectangle.RenderTransform>
</Rectangle>
</Grid>
</ControlTemplate>
但是, 此论坛帖子指出 TemplateBinding 仅适用于 FrameworkElements 的派生类。 ScaleTransform 不是 FrameworkElement。有解决办法吗?对于这种情况有什么最佳实践吗?
I'm trying to create a custom control in Silverlight that dynamically scales an element in it's ControlTemplate. First attempt of the ControlTemplate looks something like this:
<ControlTemplate TargetType="controls:ProgressBar">
<Grid>
<Rectangle x:Name="TrackPart" Fill="{TemplateBinding Background}" HorizontalAlignment="Left" />
<Rectangle x:Name="ProgressPart" Fill="Blue" >
<Rectangle.RenderTransform>
<ScaleTransform ScaleX="{TemplateBinding Progress}" />
</Rectangle.RenderTransform>
</Rectangle>
</Grid>
</ControlTemplate>
However, this forum thread states that TemplateBinding only works on derivatives of FrameworkElements. ScaleTransform is not a FrameworkElement. Is there a work around for this? Any best practices for this sort of situation out there?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以绑定 RenderTransform 本身,而不是绑定 RenderTransform 的 ScaleX 和 ScaleY 属性。
问题是源是双精度值,您需要一个 Transform。因此,您需要能够将 double 转换为 ScaleTransform。您可以创建一个 IValueConverter 来执行此操作:
您无法指定要在 TemplateBinding 中使用的 IValueConverter,因此您可以使用以relativesource 作为 TemplatedParent 的常规 Binding。像这样:
您需要将 IValueConverter 放置在 ControlTemplate 的根资源中,在 Binding 的范围内:
Rather than binding the ScaleX and ScaleY properties of the RenderTransform, you can bind the RenderTransform itself.
The problem is that the source is a double value, and you need a Transform. So you need to be able to convert a double to a ScaleTransform. You can create an IValueConverter to do that:
You can't specify an IValueConverter to use in a TemplateBinding, so you can use a regular Binding with RelativeSource as TemplatedParent. Like this:
and you need to place the IValueConverter in the resources of ControlTemplate's root, in scope of the Binding:
假设您始终使用像矩形这样的简单项目,您可以将矩形的高度和宽度绑定到进度,然后使用绑定转换器相应地调整值
Assuming that you are always using simple items like a rectangle, you could bind the rectangle's height and width to the progress, and then use a binding converter to adjust the value accordingly