我为ListView控件创建自定义视图。
public class CustomView: View{
//...
public DataTemplate ItemTemplate { get; set; }
//...
}
<CustomView x:Key="Custom">
<CustomView.ItemTemplate>
<DataTemplate>
</DataTemplate>
</CustomView.ItemTemplate>
</CustomView>
在ListViewItem
的样式中,我想从CustomView绑定
。DataTemplate
。ContentTemplate
上的ItemTemplate
<Style x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type CustomView}, ResourceId=CustomViewItem}"
TargetType="{x:Type ListViewItem}"
BasedOn="{StaticResource {x:Type ListBoxItem}}">
<Setter Property="ContentTemplate"
Value="{Binding Path=View.ItemTemplate,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border x:Name="ItemBorder">
<ContentPresenter />
</Border>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type CustomView}, ResourceId=CustomView}"
TargetType="{x:Type ListView}"
BasedOn="{StaticResource {x:Type ListBox}}">
</Style>
视图工作,但在VS的输出窗口中,我收到以下错误:
找不到引用为“RelativeSource FindAncestor,AncestorType=”系统的绑定源。窗户。控制。ListView',AncestorLevel='1'。BindingExpression:Path=View。项目模板;DataItem=null;目标元素是“ListViewItem”(名称=“”);目标属性为“ContentTemplate”(类型为“DataTemplate”)
public class CustomListView : ListView
{
static CustomListView()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomListView), new
FrameworkPropertyMetadata(typeof(CustomListView)));
}
protected override DependencyObject GetContainerForItemOverride()
{
return new CustomListViewItem();
}
}