WPF ListView与GridView样式ListViewItem

时间:2018-05-01 07:44:45

标签: wpf xaml listview gridview

好的我开始发布一个问题,但是在帖子中我有一个想法,结果证明它...我发布了一个答案,以防万一其他人沮丧。

我正在尝试为ListViewItem创建自定义样式,并在具有GridView的ListView中创建。这是我的风格:

<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:

<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”读数,而不是每列中只读一次,每次只有一次。如果我删除项容器样式,它应该工作。

1 个答案:

答案 0 :(得分:0)

所以我通过进入Blend,创建ListViewItem以及编辑模板的副本来生成此样式。然而事实证明模板是不同的,如果它在GridView内部而不是它...我不得不创建一个带有GridView的ListView,然后在其中创建一个ListViewItem,然后编辑该样式。以下是修订后的风格:

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

希望能给别人带来一些挫败......