ControlTemplate中是否可以通过xaml绑定PolyLineSegment.Points?
Is it possible to bind PolyLineSegment.Points through xaml in ControlTemplate?
最近,我需要更改 Thumb 的模板。我想像这样将其形状更改为三角形。我使用一个 Path 作为它的 ControlTemplate。这是代码:
<Thumb
Width="100"
Height="30"
HorizontalAlignment="Left"
VerticalAlignment="Top">
<Thumb.Template>
<ControlTemplate>
<Path Fill="Red">
<Path.Data>
<PathGeometry>
<PathFigure>
<PolyLineSegment>
<PolyLineSegment.Points>
<PointCollection>
<Point X="0" Y="0" />
<Point X="{TemplateBinding Width}" Y="{TemplateBinding Height}" />
<Point X="0" Y="{TemplateBinding Height}" />
</PointCollection>
</PolyLineSegment.Points>
</PolyLineSegment>
</PathFigure>
</PathGeometry>
</Path.Data>
</Path>
</ControlTemplate>
</Thumb.Template>
</Thumb>
您可以看到我尝试将 PolyLineSegment.Points 绑定到 Thumb 的宽度和高度。
但是 Visual Studio 抛出一个错误:
"Error Object of type 'System.Windows.TemplateBindingExpression'
cannot be converted to type 'System.Double'."
不知道为什么不行。谁能给我提示?
终于找到问题所在了。我发现这是因为 Point.X
或 Point.Y
不是 Dependency 属性。所以这就是绑定不起作用的原因。
我通过绑定到 LineSegment
来解决这个问题。
<ControlTemplate>
<Path Fill="{TemplateBinding Background}">
<Path.Data>
<PathGeometry>
<PathFigure StartPoint="0 0">
<LineSegment>
<LineSegment.Point>
<MultiBinding Converter="{StaticResource thumb2PointConverter}" ConverterParameter="rightBottom">
<Binding Path="Width" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=Thumb}" />
<Binding Path="Height" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=Thumb}" />
</MultiBinding>
</LineSegment.Point>
</LineSegment>
<LineSegment>
<LineSegment.Point>
<MultiBinding Converter="{StaticResource thumb2PointConverter}" ConverterParameter="leftBottom">
<Binding Path="Width" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=Thumb}" />
<Binding Path="Height" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=Thumb}" />
</MultiBinding>
</LineSegment.Point>
</LineSegment>
</PathFigure>
</PathGeometry>
</Path.Data>
</Path>
</ControlTemplate>
最近,我需要更改 Thumb 的模板。我想像这样将其形状更改为三角形。我使用一个 Path 作为它的 ControlTemplate。这是代码:
<Thumb
Width="100"
Height="30"
HorizontalAlignment="Left"
VerticalAlignment="Top">
<Thumb.Template>
<ControlTemplate>
<Path Fill="Red">
<Path.Data>
<PathGeometry>
<PathFigure>
<PolyLineSegment>
<PolyLineSegment.Points>
<PointCollection>
<Point X="0" Y="0" />
<Point X="{TemplateBinding Width}" Y="{TemplateBinding Height}" />
<Point X="0" Y="{TemplateBinding Height}" />
</PointCollection>
</PolyLineSegment.Points>
</PolyLineSegment>
</PathFigure>
</PathGeometry>
</Path.Data>
</Path>
</ControlTemplate>
</Thumb.Template>
</Thumb>
您可以看到我尝试将 PolyLineSegment.Points 绑定到 Thumb 的宽度和高度。
但是 Visual Studio 抛出一个错误:
"Error Object of type 'System.Windows.TemplateBindingExpression'
cannot be converted to type 'System.Double'."
不知道为什么不行。谁能给我提示?
终于找到问题所在了。我发现这是因为 Point.X
或 Point.Y
不是 Dependency 属性。所以这就是绑定不起作用的原因。
我通过绑定到 LineSegment
来解决这个问题。
<ControlTemplate>
<Path Fill="{TemplateBinding Background}">
<Path.Data>
<PathGeometry>
<PathFigure StartPoint="0 0">
<LineSegment>
<LineSegment.Point>
<MultiBinding Converter="{StaticResource thumb2PointConverter}" ConverterParameter="rightBottom">
<Binding Path="Width" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=Thumb}" />
<Binding Path="Height" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=Thumb}" />
</MultiBinding>
</LineSegment.Point>
</LineSegment>
<LineSegment>
<LineSegment.Point>
<MultiBinding Converter="{StaticResource thumb2PointConverter}" ConverterParameter="leftBottom">
<Binding Path="Width" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=Thumb}" />
<Binding Path="Height" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=Thumb}" />
</MultiBinding>
</LineSegment.Point>
</LineSegment>
</PathFigure>
</PathGeometry>
</Path.Data>
</Path>
</ControlTemplate>