Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF-设置无外观控件的样式:如何从第二级ControlTemplates访问控件的依赖属性?_Wpf_Dependency Properties_Controltemplate_Itemscontrol_Templatebinding - Fatal编程技术网

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}}}"