我正在扩展ItemsControl( class EnhancedItemsControl : ItemsControl),因为我想向它添加几个依赖属性 - 比如AlternativeContent当集合中没有项目时将显示这些属性(考虑在 itemscontrol 中为搜索结果输入“输入搜索词并点击搜索”标签)。
我已经子类化ItemsControl并添加了一个AlternativeContentdep。类型的属性FrameworkElement。现在我想在 Themes/Generic.xaml 中提供默认样式(我已添加ThemeInfoAttribute到 AsseblyInfo,并在静态 costructor 中提供了元数据,如本优秀教程中所述)。
样式包含. ControlTemplate_ _ControlTemplateContentPresenterAlternativeContent
现在,我的问题是如何告诉ContentPresenter它应该从顶层获取其内容EnhancedItemsControl?如果我在 style's 内部ControlTemplate,我会使用:
Content="{Binding AlternativeContent, RelativeSource={RelativeSource TemplatedParent}}"
但是由于我在ItemsControl's ControlTemplateinside style'sControlTemplate中,这显然不起作用,我需要引用的不是父模板,而是祖父模板,但是TemplateBinding没有AncestorLevel参数。
我也试过:
Content="{Binding AlternativeContent, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type WPFControls:EnhancedItemsControl}}}"
但这也会导致空ContentPresenter。我不能命名TemplatedParent(因为它在 之外ControlTemplate),所以我不能通过名称来引用它。我不能使用TemplatedParent RelativeBinding,因为它没有达到超过两个级别的控制模板。RelativeSource FindAncestor奇怪的是不起作用。
知道如何解决这个问题吗?谢谢!
Generic.xaml(摘录):
<Style TargetType="{x:Type WPFControls:EnhancedItemsControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type WPFControls:EnhancedItemsControl}">
<ItemsControl
x:Name="RootItemsControl"
ItemsSource="{Binding}"
ItemTemplate="{TemplateBinding ItemTemplate}"
Background="{TemplateBinding Background}"
HorizontalContentAlignment="Stretch"
>
<ItemsControl.Template>
<ControlTemplate>
<ScrollViewer
x:Name="ContentScrollViewer"
CanContentScroll="False"
>
<StackPanel
x:Name="InnerPanel"
>
<ItemsPresenter
Width="{Binding ActualWidth, ElementName=InnerPanel}"
MaxWidth="{Binding ActualWidth, ElementName=InnerPanel}"
HorizontalAlignment="Stretch"
/>
<ContentPresenter
Content="{Binding AlternativeContent, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type WPFControls:EnhancedItemsControl}}}"
>
<ContentPresenter.Style>
<Style>
<Setter Property="ContentPresenter.Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger
Binding="{Binding Items.Count, ElementName=RootItemsControl}"
Value="0">
<Setter Property="ContentPresenter.Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentPresenter.Style>
</ContentPresenter>
</StackPanel>
</ScrollViewer>
</ControlTemplate>
</ItemsControl.Template>
</ItemsControl>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
控制代码:
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));
}
用法(aList<string>提供为DataContext):
<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>