WPF/XAML:从 ControlTemplate.Trigger 访问 ControlTemplate 外部的元素
我有一个带有灯和开关的 WPF 应用程序。当我按下开关时,开关和灯应更改为“ON”图像,当我再次按下时,它们应更改为“OFF”图像。我有一个限制:我只能在 XAML 中严格执行此操作,因此不能使用代码隐藏文件。 我这样做的方法是重新定义 ToggleButton 的控件模板。此控制模板中只有灯开关(灯本身不应该可单击),这显然是我的问题。我无法从控制模板触发器内部访问灯开关。我收到以下错误“找不到触发器目标‘lightImage’。(目标必须出现在使用它的任何 Setter、触发器或条件之前。)”
这是我的代码:
<Image Name="lightImage" Source="Resources/LOFF.bmp" Stretch="None" Canvas.Left="82" Canvas.Top="12"/>
<ToggleButton Canvas.Left="169" Canvas.Top="123">
<ToggleButton.Template>
<ControlTemplate TargetType="ToggleButton">
<Image Name="switchImage" Source="Resources/SUp.bmp"/>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="switchImage" Property="Source" Value="Resources/SDown.bmp" />
<Setter TargetName="lightImage" Property="Source" Value="Resources/LON.bmp"/>
</Trigger>
<Trigger Property="IsChecked" Value="False">
<Setter TargetName="switchImage" Property="Source" Value="Resources/SUp.bmp"/>
<Setter TargetName="lightImage" Property="Source" Value="Resources/LOFF.bmp"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</ToggleButton.Template>
</ToggleButton>
还有其他方法可以做到这一点吗? 干杯
I have a WPF application that has a light and a switch. When I press the switch the switch and light should change to its "ON" image and when I press again they should change to their "OFF" images. I have a single restriction: I can only do this strictly in XAML and therfore no code-behind files.
The way I do this is to redefine the control template for ToggleButton. Only the light switch is in this control template (the light itself shouldn't be clickable), and that is apparently my problem. I can't access the light switch from inside the control templates triggers. I get the following error "Cannot find the Trigger target 'lightImage'. (The target must appear before any Setters, Triggers, or Conditions that use it.)"
Heres my code:
<Image Name="lightImage" Source="Resources/LOFF.bmp" Stretch="None" Canvas.Left="82" Canvas.Top="12"/>
<ToggleButton Canvas.Left="169" Canvas.Top="123">
<ToggleButton.Template>
<ControlTemplate TargetType="ToggleButton">
<Image Name="switchImage" Source="Resources/SUp.bmp"/>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="switchImage" Property="Source" Value="Resources/SDown.bmp" />
<Setter TargetName="lightImage" Property="Source" Value="Resources/LON.bmp"/>
</Trigger>
<Trigger Property="IsChecked" Value="False">
<Setter TargetName="switchImage" Property="Source" Value="Resources/SUp.bmp"/>
<Setter TargetName="lightImage" Property="Source" Value="Resources/LOFF.bmp"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</ToggleButton.Template>
</ToggleButton>
Is there another way to do this?
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎有“onImage”,但试图引用“lightImage”?
编辑:由于这些触发器位于您的控件模板内,我认为它仅在该模板内查找“lightImage”。您应该在后面的代码中为“源”创建一个属性,并将其绑定到图像和按钮中。
Edit2:如果后面没有代码,也许你可以尝试一些相对绑定:
抱歉,如果这完全是愚蠢的,我使用Silverlight,这仅在WPF中可用,所以只是一个疯狂的猜测!
不管怎样,想法来自这个备忘单,似乎你可以在 WPF 中拥有相当复杂的绑定,因此值得尝试一些不同的绑定: http://www.nbdtech.com/Free/WpfBinding.pdf
You seem to have "onImage", but trying to reference "lightImage"?
Edit: since those triggers are inside your control template I think it looks for "lightImage" only inside that template. You should create a property for 'source' in the code behind and bind to that both in your image and button.
Edit2: if no code behind maybe you could try some relative binding along the lines of:
Sorry if this is completely stupid, I use Silverlight and this is only available in WPF, so only a wild guess!
Anyway, idea comes from this cheatsheet, seems you can have quite complex bindings in WPF, so worth trying a few different ones: http://www.nbdtech.com/Free/WpfBinding.pdf
最后,我修好了。我不认为您可以在我不想点击的图像上使用“IsHitTestVisible”属性。有了这个属性,我可以将 lightImage 放入控件模板中,瞧。
代码如下:
Finally, I fixed it. I didn't consider that you could use the "IsHitTestVisible" property on the image I didn't want to be clickable. With that property I could just put the lightImage inside the controltemplate and voila.
Heres the code: