我创建了一个自定义的 silverlight 模板控件,它包含两个部分,一个 ContentControl 和一个 ItemsControl。我已经成功地在自定义模板控件中创建了一个与它们一起使用的模板,但我想将部件的内部模板公开给 Blend,以便可以编辑它们,并使用默认值公开。
我尝试了以下代码的各种变体,试图让它工作,但没有成功。
[TemplatePart(Name = ExpanderTemplateControl2.PartName_FullViewItem, Type = typeof(ExpanderFullViewItem))]
[TemplatePart(Name = ExpanderTemplateControl2.PartName_ItemsControl, Type = typeof(ItemsControl))]
public class ExpanderTemplateControl2 : Control
{
public const string PartName_FullViewItem = "PART_FullViewItem";
public const string PartName_ItemsControl = "PART_ItemsControl";
#region Parts
private ContentControl part_FullViewItem;
private ItemsControl part_ItemsControl;
#endregion
static ExpanderTemplateControl2()
{
//DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomListControl), new FrameworkPropertyMetadata(typeof(CustomListControl)));
//Add Ownership of dependency property
ItemTemplateProperty = ItemsControl.ItemTemplateProperty;//.AddOwner(typeof(ExpanderTemplateControl2));
ItemsPanelProperty = ItemsControl.ItemsPanelProperty;//.AddOwner(typeof(ExpanderTemplateControl2));
FullViewTemplateProperty = ContentControl.TemplateProperty;//.AddOwner(typeof(ExpanderTemplateControl2));
}
public ExpanderTemplateControl2()
{
this.DefaultStyleKey = typeof(ExpanderTemplateControl2);
}
#region Dependency Properties
public static readonly DependencyProperty ItemTemplateProperty;
//= DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(ExpanderTemplateControl2), new PropertyMetadata(null));
public static readonly DependencyProperty ItemsPanelProperty;
//= DependencyProperty.Register("ItemsPanel", typeof(ItemsPanelTemplate), typeof(ExpanderTemplateControl2), new PropertyMetadata(null));
public static readonly DependencyProperty FullViewTemplateProperty;
//= DependencyProperty.Register("FullViewItemTemplate", typeof(ControlTemplate), typeof(ExpanderTemplateControl2), new PropertyMetadata(null));
#endregion
#region Properties
[BindableAttribute(false)]
public DataTemplate ItemTemplate
{
get { return (DataTemplate)GetValue(ItemTemplateProperty); }
set { SetValue(ItemTemplateProperty, value); }
}
[BindableAttribute(false)]
public ItemsPanelTemplate ItemsPanel
{
get { return (ItemsPanelTemplate)GetValue(ItemsPanelProperty); }
set { SetValue(ItemsPanelProperty, value); }
}
[BindableAttribute(false)]
public ControlTemplate FullViewTemplate
{
get { return (ControlTemplate)GetValue(FullViewTemplateProperty); }
set { SetValue(FullViewTemplateProperty, value); }
}
#endregion
}
谁能给我一些指示?
干杯
特里斯坦
更新:通过执行以下操作,我设法使其处于某种工作状态,但我对此并不满意。有没有更好的办法?
#region Dependency Properties
public static readonly DependencyProperty ItemTemplateProperty
= DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(ExpanderTemplateControl2), new PropertyMetadata(null));
public static readonly DependencyProperty ItemsPanelProperty
= DependencyProperty.Register("ItemsPanel", typeof(ItemsPanelTemplate), typeof(ExpanderTemplateControl2), new PropertyMetadata(null));
public static readonly DependencyProperty FullViewTemplateProperty
= DependencyProperty.Register("FullViewTemplate", typeof(ControlTemplate), typeof(ExpanderTemplateControl2), new PropertyMetadata(null));
#endregion
#region Properties
[BindableAttribute(false)]
public DataTemplate ItemTemplate
{
get
{
var result = (DataTemplate)GetValue(ItemTemplateProperty);
if (result == null)
{
SetValue(ItemTemplateProperty, part_ItemsControl.ItemTemplate);
result = part_ItemsControl.ItemTemplate;
}
return result;
}
set
{
part_ItemsControl.ItemTemplate = value;
SetValue(ItemTemplateProperty, value);
}
}
[BindableAttribute(false)]
public ItemsPanelTemplate ItemsPanel
{
get
{
var result = (ItemsPanelTemplate)GetValue(ItemsPanelProperty);
if (result == null)
{
SetValue(ItemsPanelProperty, part_ItemsControl.ItemsPanel);
result = part_ItemsControl.ItemsPanel;
}
return result;
}
set
{
part_ItemsControl.ItemsPanel = value;
SetValue(ItemsPanelProperty, value);
}
}
[BindableAttribute(false)]
public ControlTemplate FullViewTemplate
{
get
{
var result = (ControlTemplate)GetValue(FullViewTemplateProperty);
if (result == null)
{
SetValue(FullViewTemplateProperty, part_FullViewItem.Template);
result = part_FullViewItem.Template;
}
return result;
}
set
{
part_FullViewItem.Template = value;
SetValue(FullViewTemplateProperty, value);
}
}
#endregion