在下面的代码中,MousePressImage
是类ButtonControl
的依赖项属性。
以下绑定不起作用。感谢您帮助解决此问题。
Value="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=MousePressImage}"/>
<Style TargetType="{x:Type local:ButtonControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ButtonControl}">
<Border>
<Image x:Name="img"
Source="pack://application:,,,/Recipe_06_13;component/Resources/normal.bmp"
/>
</Border>
<!--<Border x:Name="border">
<Border.Background>
<ImageBrush x:Name="img"
ImageSource="/Recipe_06_13;component/Resources/fatal.png"/>
</Border.Background>
</Border>-->
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="img"
Property="Source"
Value="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=MousePressImage}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我像这样创建ButtonControl
。
<local:ButtonControl Height="48" Width="160"
MouseOverImage="pack://application:,,,/Recipe_06_13;component/Resources/Over.bmp"
MousePressImage="pack://application:,,,/Recipe_06_13;component/Resources/Press.bmp"
DisableImage=" ">
</local:ButtonControl>
答案 0 :(得分:6)
因为您的触发器位于ControlTemplate上,所以您需要从模拟控制实例获取MousePressImage 。为此,请使用TemplateBinding
或(更可靠)RelativeSource TemplatedParent
:
<Setter TargetName="img"
Property="Source"
Value="{Binding RelativeSource={RelativeSource TemplatedParent},
Path=MousePressImage}" />