Image.Height TemplateBinding does not work

99封情书 提交于 2019-12-20 03:54:29

问题


I have created a CustomControl implemented from Button class in WPF.

public class ImageButton : Button
{
      ...

       public int ImageHeight
       {
           get { return (int)GetValue(ImageHeightProperty); }
           set { SetValue(ImageHeightProperty, value); }
       }
       public static readonly DependencyProperty ImageHeightProperty =
           DependencyProperty.Register("ImageHeight", typeof(int), typeof(ImageButton), new UIPropertyMetadata(32)); 

       ...
}

And I have resource template like this:

<Setter Property="Template">
   <Setter.Value>
     <ControlTemplate TargetType="{x:Type custom:ImageButton}">
       <Border>
         <StackPanel>
          <Image Name="image" Height="{TemplateBinding ImageHeight}"/>
          <TextBlock Text="{TemplateBinding Text}" />
         </StackPanel>
       </Border>
     <ControlTemplate.Triggers>
   </ControlTemplate>
 </Setter.Value>

ImageHeight dependecy property doesn't binding. When I write like as below it works successful.

Height="32"

What is wrong with this?


回答1:


Did you try using {Binding RelativeSource={RelativeSource TemplatedParent}, Path=Progress} instead ?

See these answers for more details...

WPF TemplateBinding vs RelativeSource TemplatedParent

Binding custom dependency property to custom WPF style

hope this helps



来源:https://stackoverflow.com/questions/15435385/image-height-templatebinding-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!