【问题标题】:Image.Height TemplateBinding does not workImage.Height TemplateBinding 不起作用
【发布时间】:2013-03-15 14:49:04
【问题描述】:

我在 WPF 中创建了一个从 Button 类实现的 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 依赖属性不绑定。 当我像下面这样写时,它会成功。

Height="32"

这有什么问题?

【问题讨论】:

    标签: wpf wpf-controls custom-controls dependency-properties


    【解决方案1】:

    您是否尝试改用{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Progress}

    查看这些答案了解更多详情...

    WPF TemplateBinding vs RelativeSource TemplatedParent

    Binding custom dependency property to custom WPF style

    希望对你有帮助

    【讨论】:

    • 成功了。谢谢。在同一个控件中,我有&lt;TextBlock Text="{TemplateBinding Text}" /&gt;Text 也是一个 DependecyPropety,它可以正常工作而不会出现任何错误。我不明白这 2 dp 之间有什么区别。
    • 这就是第一个链接 - 一个是编译器时,另一个是运行时 - XAML 模板似乎无法完全识别自定义属性。另一个稍后绑定。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多