访问 ControlTemplate 中的内部元素
这是我的代码:
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border x:Name="Chrome">
<Grid x:Name="Arrow">
<Grid.Background>
<DrawingBrush Viewport="0,0,4,4" Viewbox="0,-0.4,16,16" ViewboxUnits="Absolute">
<DrawingBrush.Drawing>
<GeometryDrawing x:Name="ArrowDrawing">
<GeometryDrawing.Geometry>
<PathGeometry>
<PathFigure StartPoint="1,1" IsClosed="True">
<LineSegment Point="2,2.45"/>
<LineSegment Point="3,1"/>
<LineSegment Point="2,1.75"/>
</PathFigure>
</PathGeometry>
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingBrush.Drawing>
</DrawingBrush>
</Grid.Background>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Brush" TargetName="ArrowDrawing" Value="{StaticResource DisabledForecolor}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
因此,当触发器触发时(在编译时),我收到错误:
找不到触发器目标“ArrowDrawing”。 (目标必须出现在使用它的任何 Setters、触发器或条件之前。)
我如何从触发器实际访问名为 ArrowDrawing
的 GeometryDrawing
?
Here goes my code:
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border x:Name="Chrome">
<Grid x:Name="Arrow">
<Grid.Background>
<DrawingBrush Viewport="0,0,4,4" Viewbox="0,-0.4,16,16" ViewboxUnits="Absolute">
<DrawingBrush.Drawing>
<GeometryDrawing x:Name="ArrowDrawing">
<GeometryDrawing.Geometry>
<PathGeometry>
<PathFigure StartPoint="1,1" IsClosed="True">
<LineSegment Point="2,2.45"/>
<LineSegment Point="3,1"/>
<LineSegment Point="2,1.75"/>
</PathFigure>
</PathGeometry>
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingBrush.Drawing>
</DrawingBrush>
</Grid.Background>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Brush" TargetName="ArrowDrawing" Value="{StaticResource DisabledForecolor}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
So when the trigger triggers (on compile time), I got the error:
Cannot find the Trigger target 'ArrowDrawing'. (The target must appear before any Setters, Triggers, or Conditions that use it.)
How do I actually access that GeometryDrawing
named ArrowDrawing
from the trigger?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上有两种方法可以实现这一点,一种是使用绑定:
另一种是使用 XAML 内部引用:
相同的答案是 这里!
Actually there is two ways to achieve this, one is using a binding:
and the other one is to use a XAML internal reference:
the same answer is here!
这篇文章可能会有所帮助
This post might help