依赖属性和绑定的问题。“属性"ImgSource"不是DependencyProperty”
属性"ImgSource"不是DependencyProperty。若要在标记中使用,目标类型必须通过可访问的实例属性"ImgSource"来公开非附加属性。对于附加属性,声明的类型必须提供静态的"GetImgSource"和"SetImgSource"方法。!
参考方法
MSDN上网友的解答
分析原因:Image不是继承自FrameworkElement的,templatebinding不好用。
解决办法
使用Binding
示例:
<Style x:Key="xButton" TargetType="local:MyButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:MyButton">
<StackPanel Orientation="Horizontal" Width="250">
<Image Source="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=ImgSource}" Width="50" Height="25"/>
<ContentPresenter x:Name="contentPresenter" Content="{TemplateBinding Content}"
Foreground="White" VerticalAlignment="Center"/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>