【问题标题】:XAML control to inherit from templates从模板继承的 XAML 控件
【发布时间】:2010-03-01 11:07:36
【问题描述】:

在构建自定义用户控件时,可以从哪些控件继承具有 Children 属性并支持模板化。

目前我知道Panel,但它不支持DefaultStyleKeyGetTemplateChild()等属性和方法;

是否有一个我可以继承模板的接口,例如:

public class Scroller : Panel, ITemplates //Something like ITemplates
{
   public override void OnApplyTemplate()
   {
        base.OnApplyTemplate();
        container = GetTemplateChild("container") as StackPanel; //I want to be able to do this

        this.Children; //And also be able to use this
   }
}

【问题讨论】:

    标签: silverlight xaml


    【解决方案1】:

    您需要 ItemsControl。它有一个 Items 属性,它的模板非常灵活,TabControl、ListBox、ComboBox 等都派生自它。

    【讨论】:

    • 出于某种原因,这就是以前的内容,但它不起作用。我将在 ItemsPanelTemplate 中使用我的自定义控件作为列表框。当我从 ItemsControl 继承时,它告诉我需要 Panel 的根目录。
    • 正确,如果您尝试覆盖 ListBox 的布局,那么您必须创建一个面板并且您没有获得模板。面板没有视觉表示,它只是布置其子级,因此不需要模板。
    • 您应该决定是要创建自定义 ItemsControl(换句话说,没有 ListBox),还是只想覆盖 ListBox 的布局。
    • 我想我会将它用作自定义 ItemsControl。就像 ListBox 项如何被称为 ListItems 一样,我如何称呼这个控件的项?我将附加一个生成的项目模板并将其绑定到一个 ItemSource。
    猜你喜欢
    • 1970-01-01
    • 2013-09-03
    • 2013-12-13
    • 2018-01-18
    • 1970-01-01
    • 2018-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多