WPF ListView GroupStyle GridViewHeaderRowPresenter不隐藏原始标头

时间:2016-10-12 21:50:44

标签: c# wpf listview

enter image description here

我有一个使用GroupStyles的列表视图,其中ColumnHeaders显示在每个组中。这很好用。

但我想隐藏原始 ListView ColumnHeader(见图片顶部)并在组内显示标题。

如何将它们分开,以便只显示在组中?

GroupStyle:

<Style x:Key="GroupingSerialStyle" TargetType="{x:Type GroupItem}" BasedOn="{StaticResource GroupingBase}">
    <Setter Property = "Template">
        <Setter.Value>
            <ControlTemplate>
                <Expander Background="{TemplateBinding Background}"  BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" IsExpanded="{Binding IsExpandedAll, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Mode=OneTime}" Margin="2">
                    <Expander.Header>
                        <StackPanel Orientation = "Horizontal">
                            <TextBlock Text=" # "/>
                            <TextBlock Text = "{Binding Name}" />
                        </StackPanel>
                    </Expander.Header>
                    <StackPanel Orientation="Vertical">
                        <GridViewHeaderRowPresenter Visibility = "Visible" Margin="15,0,0,0"
                DataContext="{Binding View, RelativeSource={RelativeSource FindAncestor,ListView,1}}"
                Columns="{Binding Columns}"       
                ColumnHeaderContainerStyle="{StaticResource GridViewColumnHeaderStyleABCD}"
                ColumnHeaderTemplate="{Binding ColumnHeaderTemplate}"
                ColumnHeaderTemplateSelector="{Binding ColumnHeaderTemplateSelector}"
                AllowsColumnReorder="{Binding AllowsColumnReorder}"
                ColumnHeaderContextMenu="{Binding ColumnHeaderContextMenu}"
                ColumnHeaderToolTip="{Binding ColumnHeaderToolTip}"
                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        <ItemsPresenter Margin = "15,0,0,0" />
                    </StackPanel>
                </Expander>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

修改:此外,列标题应为每列显示自定义颜色。

2 个答案:

答案 0 :(得分:2)

可以通过ColumnHeaderContainerStyle来解决。如果您在Template设置空GridView.ColumnHeaderContainerStyle并使用样式GridViewColumnHeaderStyleABCD作为GroupItem它应该有效。

我尝试了以下操作,只显示具有自定义Background的组项目的列标题。

<ListView BindingGroup="{Binding Name}" ItemsSource="{Binding Source={StaticResource TestItems}}">
    <!-- ### custom coloring column headers ### -->
    <ListView.Resources>
        <Style TargetType="GridViewColumnHeader">
            <Setter Property="Background" Value="{Binding Path=Column.(YourNamespace:BgHelper.CustomBackground), RelativeSource={RelativeSource Self}}" />
        </Style>
    </ListView.Resources>
    <ListView.View>
        <GridView>
            <GridView.ColumnHeaderContainerStyle>
                <Style TargetType="GridViewColumnHeader">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
                                <!-- Empty: ListViews header shows nothing -->
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </GridView.ColumnHeaderContainerStyle>
            <!-- ### custom coloring column headers ### -->
            <GridViewColumn DisplayMemberBinding="{Binding Parameter0}" Header="Par0" YourNamespace:BgHelper.CustomBackground="Pink" />
            <GridViewColumn DisplayMemberBinding="{Binding Parameter1}" Header="Par1" YourNamespace:BgHelper.CustomBackground="Green" />
        </GridView>
    </ListView.View>
    <ListView.GroupStyle>
        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <Expander Margin="2">
                        <Expander.Header>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text=" # " />
                                <TextBlock Text="{Binding Name}" />
                            </StackPanel>
                        </Expander.Header>
                        <StackPanel Orientation="Vertical">
                            <GridViewHeaderRowPresenter Margin="15,0,0,0"
                                                    Columns="{Binding Columns}"
                                                    DataContext="{Binding View, RelativeSource={RelativeSource FindAncestor, ListView, 1}}"
                                                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                                    Visibility="Visible" />
                            <ItemsPresenter Margin="15,0,0,0" />
                        </StackPanel>
                    </Expander>
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
        </GroupStyle>
    </ListView.GroupStyle>
</ListView>

要自定义列标题颜色,可以使用AttachedProperty,如下所示:

namespace YourNamespace
{
    public class BgHelper
    {
        #region Fields

        public static DependencyProperty CustomBackgroundProperty =
            DependencyProperty.RegisterAttached("CustomBackground",
                                                typeof (SolidColorBrush),
                                                typeof (BgHelper),
                                                new FrameworkPropertyMetadata(new SolidColorBrush(Colors.Transparent)));

        #endregion

        #region Public Methods

        public static SolidColorBrush GetCustomBackground(DependencyObject element)
        {
            return (SolidColorBrush) element.GetValue(CustomBackgroundProperty);
        }

        public static void SetCustomBackground(DependencyObject element, SolidColorBrush value)
        {
            element.SetValue(CustomBackgroundProperty, value);
        }

        #endregion
    }
}

然后我通过设置CustomBackground扩展了上面的示例,并以GridViewColumnHeader样式使用它。我用注释标记了新的/编辑过的行。

答案 1 :(得分:0)

@WPFGermany是对的。但是,在我原来的问题中,我忘了添加对于GroupStyle中的标题我还需要为某些列标题显示不同的背景颜色。我能够通过复制GroupStyle中的所有列并将每个ColumnHeaderStyle指向一个唯一的颜色来实现这一点。也许有更好的方法,但现在这对我有用。以下是部分GroupStyle来说明:

           <Style x:Key="GroupingNcpCheckStyleRoot" TargetType="{x:Type GroupItem}" BasedOn="{StaticResource GroupingBaseRoot}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Expander Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" IsExpanded="{Binding IsExpandedData, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Mode=OneTime}" Margin="2">
                            <Expander.Header>
                                <StackPanel Orientation="Horizontal">
                                    <CheckBox x:Name="HeaderCheckBox" Margin="0,0,6,0" IsChecked="True"></CheckBox>
                                    <TextBlock Text=" # "/>
                                    <TextBlock Text="{Binding Name}"/>
                                </StackPanel>
                            </Expander.Header>
                            <StackPanel Orientation="Vertical">
                                <GridViewHeaderRowPresenter Margin="15,0,0,0"

                                                DataContext="{Binding View, RelativeSource={RelativeSource FindAncestor, ListView, 1}}" 
                                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                                Visibility="Visible"   >
                                    <GridViewHeaderRowPresenter.Columns>
                                    <!--NOTE: must explicitly create the collection-->
                                    <GridViewColumnCollection>
                                            <GridViewColumn Header=" " Width="60" >
                                                <GridViewColumn.HeaderContainerStyle>
                                                    <Style TargetType="{x:Type GridViewColumnHeader}" BasedOn="{StaticResource GridViewColumnHeaderStyleTEST1}">
                                                        <Setter Property="Background" Value="{StaticResource TargetHeaderColor}" />
                                                        <Setter Property="Foreground" Value="White" />
                                                        <Setter Property="BorderThickness" Value="0" />
                                                        <Setter Property="Margin" Value="-1,0,-1,0" />
                                                    </Style>
                                                </GridViewColumn.HeaderContainerStyle>
                                                <GridViewColumn.CellTemplate>
                                                <DataTemplate >
                                                    <TextBlock HorizontalAlignment="Left" Text="{Binding Path=Id, StringFormat='# {0}'}"/>
                                                </DataTemplate>
                                            </GridViewColumn.CellTemplate>
                                        </GridViewColumn>

                                            <GridViewColumn Header=" Show " >
                                                <GridViewColumn.HeaderContainerStyle>
                                                    <Style TargetType="{x:Type GridViewColumnHeader}" BasedOn="{StaticResource GridViewColumnHeaderStyleTEST1}">
                                                        <Setter Property="Background" Value="{StaticResource DifferentHeaderColor}" />
                                                        <Setter Property="Foreground" Value="White" />
                                                        <Setter Property="BorderThickness" Value="0" />
                                                        <Setter Property="Margin" Value="-1,0,-1,0" />
                                                    </Style>
                                                </GridViewColumn.HeaderContainerStyle>
                                                <GridViewColumn.CellTemplate>
                                                    <DataTemplate >
                                                        <CheckBox IsChecked="{Binding Path=IncludeInReport}" HorizontalAlignment="center"></CheckBox>
                                                    </DataTemplate>
                                                </GridViewColumn.CellTemplate>
                                            </GridViewColumn>