WPF ListView with GridView styling ListViewItem
好的,我开始发布问题,但在发布过程中,我有一个想法可以解决此问题...我正在发布答案,以防其他人感到沮丧。
我正在尝试为ListViewItem以及在具有GridView的ListView中创建自定义样式。 这是我的风格:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | <Style x:Key="StListViewItemBase" TargetType="{x:Type ListViewItem}"> <Setter Property="SnapsToDevicePixels" Value="True"/> <Setter Property="Padding" Value="4,1"/> <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> <Setter Property="Background" Value="Transparent"/> <Setter Property="BorderBrush" Value="Transparent"/> <Setter Property="BorderThickness" Value="1"/> <Setter Property="Cursor" Value="Hand" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListViewItem}"> <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true"> <ContentPresenter Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> </Border> <ControlTemplate.Triggers> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsMouseOver" Value="True"/> </MultiTrigger.Conditions> <Setter Property="Background" TargetName="Bd" Value="{StaticResource BrSelectableItemHover}"/> </MultiTrigger> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="Selector.IsSelectionActive" Value="False"/> <Condition Property="IsSelected" Value="True"/> </MultiTrigger.Conditions> <Setter Property="Background" TargetName="Bd" Value="{StaticResource BrSelectableItemHover}"/> </MultiTrigger> <Trigger Property="IsEnabled" Value="False"> <Setter Property="TextElement.Foreground" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> |
然后我有一个这样的ListView:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <ListView ItemsSource="{Binding Tabs}" ItemContainerStyle="{StaticResource StListViewItemBase}"> <ListView.View> <GridView> <GridView.Columns> <GridViewColumn Header="test"> <GridViewColumn.CellTemplate> <DataTemplate DataType="{x:Type local:SampleObject}"> <TextBlock Text="{Binding Title}" /> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> <GridViewColumn Header="test 2"> <GridViewColumn.CellTemplate> <DataTemplate DataType="{x:Type local:SampleObject}"> <TextBlock Text="{Binding Title}" /> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> </GridView.Columns> </GridView> </ListView.View> </ListView> |
但是,当我运行它时,我得到了列,但是在项目列表中,它仅给出了每个项目的'TestApp.SampleObject'读数,甚至在每个列中都没有,只是每个列一次。 如果我删除项目容器样式,则它应能正常工作。
因此,我通过进入Blend,创建ListViewItem并编辑模板的副本来生成此样式。 但是事实证明,如果模板在GridView内,模板与在GridView内的模板不同。我必须创建一个带有GridView的ListView,然后在其中创建一个ListViewItem,然后编辑该样式。 这是有效的修订样式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | <Style x:Key="StListViewItemBase" TargetType="{x:Type ListViewItem}"> <Setter Property="TextElement.FontFamily" Value="Resources/#Artifakt Element Light" /> <Setter Property="FontSize" Value="11pt" /> <Setter Property="Background" Value="Transparent"/> <Setter Property="BorderBrush" Value="Transparent"/> <Setter Property="BorderThickness" Value="0"/> <Setter Property="Margin" Value="0"/> <Setter Property="Padding" Value="5,2,5,2"/> <Setter Property="VerticalContentAlignment" Value="Center"/> <Setter Property="Foreground" Value="{StaticResource BrDarkText}" /> <Setter Property="Cursor" Value="Hand" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListViewItem}"> <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true"> <Border x:Name="InnerBorder" BorderThickness="1" CornerRadius="1"> <Grid> <Grid.RowDefinitions> <RowDefinition MaxHeight="11"/> <RowDefinition/> </Grid.RowDefinitions> <Rectangle x:Name="UpperHighlight" Fill="#75FFFFFF" Visibility="Collapsed"/> <GridViewRowPresenter Grid.RowSpan="2" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> </Grid> </Border> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter Property="Background" Value="{StaticResource BrSelectableItemHover}"/> </Trigger> <Trigger Property="IsSelected" Value="true"> <Setter Property="Background" Value="{StaticResource BrSelectableItemHover}"/> </Trigger> <Trigger Property="IsEnabled" Value="false"> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> |
希望这可以使别人免于沮丧...