Image.Height TemplateBinding不起作用(Image.Height Temp

2019-08-17 15:05发布

我创建从Button类在WPF实现的CustomControl。

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)); 

       ...
}

我有这样的资源模板:

<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属性不具有约束力。 当我写像下面它的工作原理成功。

Height="32"

有什么不对呢?

Answer 1:

你有没有尝试使用{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Progress}呢?

见这些问题的答案了解更多详情...

WPF TemplateBinding VS的RelativeSource TemplatedParent

结合自定义依赖属性自定义WPF风格

希望这可以帮助



文章来源: Image.Height TemplateBinding does not work