未调用OnApplyTemplate(),Template属性中的内容也不会显示

时间:2011-11-01 23:20:39

标签: c# wpf silverlight templates controls

我有以下课程:

namespace Controls
{
    public class DropDownButton : Control
    {
        public DropDownButton()
        {
            this.DefaultStyleKey = typeof(DropDownButton);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
        }
    }
}

在XAML中:

<Style TargetType="Controls:DropDownButton">
    <Setter Property="FontSize" Value="10"/>
    <Setter Property="Height" Value="202"/>
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="VerticalAlignment" Value="Center"/>

    <Setter Property="Template">
        <Setter.Value>
            <Button Content="Button1">
                <Button.Template>
                    <ControlTemplate TargetType="Button">
                    </ControlTemplate>
                </Button.Template>
            </Button>
        </Setter.Value>
    </Setter>
    <Setter Property="MinWidth" Value="65"/>
</Style>

我有两个问题(可能是相关的):xaml中的按钮没有显示在控件中,并且控件没有调用它的OnApplyTemplate()函数。控件似乎唯一能够响应的是设置其尺寸......

1 个答案:

答案 0 :(得分:2)

Template缺少ControlTemplate元素:

<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate> <!-- This -->
            <Button Content="Button1">
                <!-- ... -->
            </Button>
        </ControlTemplate>
    </Setter.Value>
</Setter>