我创建了一个自定义的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