WPF-设置无外观控件的样式:如何从第二级ControlTemplates访问控件的依赖属性?
我正在扩展WPF-设置无外观控件的样式:如何从第二级ControlTemplates访问控件的依赖属性?,wpf,dependency-properties,controltemplate,itemscontrol,templatebinding,Wpf,Dependency Properties,Controltemplate,Itemscontrol,Templatebinding,我正在扩展ItemsControl(class-EnhancedItemsControl:ItemsControl),因为我想向它添加几个依赖性属性,如AlternativeContent,当集合中没有项时将显示这些属性(考虑在itemscontrol中输入搜索词并点击搜索”标签以获得搜索结果) 我对ItemsControl进行了子类化,并向其添加了AlternativeContentdep.property类型FrameworkElement。现在我想在Themes/Generic.xaml中
ItemsControl
(class-EnhancedItemsControl:ItemsControl
),因为我想向它添加几个依赖性属性,如AlternativeContent
,当集合中没有项时将显示这些属性(考虑在itemscontrol中输入搜索词并点击搜索”标签以获得搜索结果)
我对ItemsControl
进行了子类化,并向其添加了AlternativeContent
dep.property类型FrameworkElement
。现在我想在Themes/Generic.xaml中提供默认样式(我向AsseblyInfo添加了ThemeInfo属性
,并在静态构造函数中提供了元数据,如本文所述)
该样式包含一个ControlTemplate
,我需要在ItemsControl模板内部使用第二个ControlTemplate
,其中我添加了一个ContentPresenter
,它应该显示可选内容
现在,我的问题是如何告诉ContentPresenter
它应该从顶级enhancedItemControl
获取其内容?如果我在style的控件模板中,我会使用:
Content="{Binding AlternativeContent, RelativeSource={RelativeSource TemplatedParent}}"
但是,由于我在ItemsControl
的ControlTemplate
内部样式的ControlTemplate
中,这显然不起作用,我需要引用的不是父模板,而是祖辈模板,但是,TemplateBinding
没有antestorlevel
参数
我还尝试:
Content="{Binding AlternativeContent, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type WPFControls:EnhancedItemsControl}}}"
但这也会导致空的ContentPresenter
。我无法命名TemplatedParent
(因为它位于ControlTemplate
之外),因此我不能按名称引用它。我不能使用TemplatedParent
RelativeBinding
,因为这不能达到两个级别的控制模板。而且RelativeSource
FindAncestor
奇怪地不起作用
知道怎么解决吗?谢谢
Generic.xaml(摘录):
用法(一个列表作为DataContext
提供):
替代内容
哎呀,我的错
Content="{Binding AlternativeContent, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type WPFControls:EnhancedItemsControl}}}"
实际上是可行的(但我的标记比示例中的标记更复杂,并且由于其他错误而没有显示内容…这是一项非常酷的工作,我很高兴你能分享它。我想知道它在我的WP7手机上是否可行。。。
public class EnhancedItemsControl : ItemsControl
{
static EnhancedItemsControl()
{
DefaultStyleKeyProperty.OverrideMetadata(
typeof(EnhancedItemsControl),
new FrameworkPropertyMetadata(typeof(EnhancedItemsControl)));
}
public FrameworkElement AlternativeContent
{
get { return (FrameworkElement)GetValue(AlternativeContentProperty); }
set { SetValue(AlternativeContentProperty, value); }
}
// Using a DependencyProperty as the backing store for AlternativeContent. This enables animation, styling, binding, etc...
public static readonly DependencyProperty AlternativeContentProperty =
DependencyProperty.Register("AlternativeContent", typeof(FrameworkElement), typeof(EnhancedItemsControl), new UIPropertyMetadata(null));
}
<WPFControls:EnhancedItemsControl Height="120" x:Name="EnhancedCollection"
>
<WPFControls:EnhancedItemsControl.AlternativeContent>
<WPFControls:CenteredLabel>
Alternative content
</WPFControls:CenteredLabel>
</WPFControls:EnhancedItemsControl.AlternativeContent>
<WPFControls:EnhancedItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" />
</DataTemplate>
</WPFControls:EnhancedItemsControl.ItemTemplate>
</WPFControls:EnhancedItemsControl>
Content="{Binding AlternativeContent, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type WPFControls:EnhancedItemsControl}}}"