积极答复者
用于类型“CardViewItem”的样式不能应用于类型“ContentPresenter”。

问题
-
<Style x:Key="{x:Type local:CardViewItem}" TargetType="{x:Type local:CardViewItem}"> <Setter Property="BorderThickness" Value="1"/> <Setter Property="BorderBrush" Value="Black"/> <Setter Property="Margin" Value="2"/> <Setter Property="Background" Value="Red"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:CardViewItem}"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Margin="{TemplateBinding Margin}"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition/> </Grid.RowDefinitions> <Button Grid.Row="0" Content="Full View" Height="24"/> <ContentPresenter Grid.Row="1" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> </Grid> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style x:Key="{x:Type local:CardView}" TargetType="{x:Type local:CardView}"> <Setter Property="ItemsPanel"> <Setter.Value> <ItemsPanelTemplate> <WrapPanel IsItemsHost="True"/> </ItemsPanelTemplate> </Setter.Value> </Setter> <Setter Property="IsSynchronizedWithCurrentItem" Value="False"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:CardView}"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <ScrollViewer HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"> <Grid> <ContentPresenter x:Name="PART_SelectedItemContentHost" Content="{TemplateBinding SelectedItem}" Visibility="Collapsed"/> <ItemsPresenter x:Name="PART_AllItemsContentHost"/> </Grid> </ScrollViewer> </Border> <ControlTemplate.Triggers> <Trigger Property="CardMode" Value="False"> <Setter Property="Panel.ZIndex" TargetName="PART_SelectedItemContentHost" Value="1"/> <Setter Property="Visibility" TargetName="PART_SelectedItemContentHost" Value="Visible"/> <Setter Property="Visibility" TargetName="PART_AllItemsContentHost" Value="Collapsed"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>
当在CardView的style中加入
<Setter Property="ItemContainerStyle" Value="{DynamicResource {x:Type local:CardViewItem}}"/>
程序运行出错,"用于类型“CardViewItem”的样式不能应用于类型“ContentPresenter”。" 为什么?
后台代码如下:
//CardViewItem.cs public class CardViewItem : ContentControl { static CardViewItem() { DefaultStyleKeyProperty.OverrideMetadata(typeof(CardViewItem), new FrameworkPropertyMetadata(typeof(CardViewItem))); } } //CardView.cs [StyleTypedProperty(Property = "ItemContainerStyle", StyleTargetType = typeof(CardViewItem))] public class CardView : Selector {...}
答案
-
根据MSDN上关于ItemContainerStyle的解释,我觉得可能是你的CardViewItem或CardView实现有问题,CardViewItem样式无法作为CardView的Item样式被应用
http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemcontainerstyle.aspx
http://msdn.microsoft.com/en-us/library/system.windows.controls.listbox.itemcontainerstyle(v=vs.95).aspx- 已标记为答案 Franklin ChenMicrosoft employee, Moderator 2013年9月9日 9:36