WPF如何设置扩展器的实际标题部分,而不仅仅是样式

时间:2019-03-31 23:52:06

标签: wpf

我有一个WPF项目控件,我想在这里使用扩展器对项目进行分组。

我有以下标记...

        <Window x:Class="ItemsControlGrouped.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:ItemsControlGrouped"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">
      <Window.Resources>
        <Style x:Key="ContainerStyle" TargetType="{x:Type GroupItem}">
          <Setter Property="Template">
            <Setter.Value>
              <ControlTemplate>
                <Expander IsExpanded="True" HorizontalAlignment="Stretch">
                  <Expander.Header>

                    <StackPanel Orientation="Horizontal" Background="LightGray" HorizontalAlignment="Stretch">
                      <TextBlock Text="{Binding Name}"  />
                      <TextBlock Margin="5,0,0,0" Text="{Binding ItemCount}" />
                      <TextBlock Text=" item(s)"  />
                    </StackPanel>

                  </Expander.Header>
                  <ItemsPresenter />
                  </Expander>
              </ControlTemplate>
            </Setter.Value>
          </Setter>
        </Style>
      </Window.Resources>
      <ItemsControl Name="list">
        <ItemsControl.GroupStyle>
          <GroupStyle ContainerStyle="{StaticResource ContainerStyle}"/>
        </ItemsControl.GroupStyle>
        <ItemsControl.ItemTemplate>
          <DataTemplate>
            <Border Margin="5" Padding="0" BorderThickness="1"  Background="LightBlue" CornerRadius="4">
              <TextBlock Padding="5" Text="{Binding Name}"/>
            </Border>
          </DataTemplate>
        </ItemsControl.ItemTemplate>

        <ItemsControl.ItemsPanel>
          <ItemsPanelTemplate>
            <WrapPanel Orientation="Horizontal"></WrapPanel>
          </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
      </ItemsControl>
    </Window>

我希望为扩展器面板设置样式,例如,我希望整个扩展器行都具有背景色,也​​许还有填充等。

在Visual Explorer中,所有这些似乎都在HeaderSite区域中,如下所示...

enter image description here

因此,我想在红色高亮区域添加背景(不仅仅是文本框,因为我可以在模板中添加圆顶。

有什么办法吗?

在此先感谢您的帮助

1 个答案:

答案 0 :(得分:1)

是的,您是正确的。要为扩展器的该部分设置样式,您需要深入了解ControlTemplate。下面显示的是一个基本示例,类似于我使用了几年的ToggleButton和Expander的自定义样式。

    <Style x:Key="ExpanderToggleButton"
             TargetType="{x:Type ToggleButton}">
        <Setter Property="UseLayoutRounding"
                    Value="True"/>
        <Setter Property="SnapsToDevicePixels"
                    Value="True"/>
        <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Grid Height="24" Width="24">
                            <Border x:Name="border"
                                            CornerRadius="12"
                                            Background="Black"/>
                            <Path x:Name="arrow" Fill="White"
                                        Data="M4.5 9 l4 0 l3.5 4.6667 l3.5 -4.6667 l4 0 l-7.5 10 z"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver"
                                             Value="True">
                                <Setter TargetName="border"
                                                Property="Background"
                                                Value="DarkGray"/>
                                <Setter TargetName="arrow"
                                                Property="Fill"
                                                Value="LightGray"/>
                            </Trigger>
                            <Trigger Property="IsChecked"
                                             Value="True">
                                <Setter TargetName="border"
                                                Property="Background"
                                                Value="White"/>
                                <Setter TargetName="arrow"
                                                Property="Fill"
                                                Value="Black"/>
                                <Setter TargetName="arrow"
                                                Property="Data"
                                                Value="M4.5 15 l4 0 l3.5 -4.6667 l3.5 4.6667 l4 0 l-7.5 -10 z"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
    </Style>

    <Style TargetType="{x:Type Expander}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Expander}">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition x:Name="ContentRow" Height="0" />
                        </Grid.RowDefinitions>
                        <Border x:Name="Border"
                                    Grid.Row="0"
                                    BorderThickness="0"
                                    CornerRadius="2,2,0,0"
                                    Background="White">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="25" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <ToggleButton Grid.Column="0" OverridesDefaultStyle="True"
                                                Style="{StaticResource ExpanderToggleButton}"
                                                IsChecked="{Binding IsExpanded, Mode=TwoWay,
                                RelativeSource={RelativeSource TemplatedParent}}">

                                </ToggleButton>
                                <!--This is the border that puts the styling around the Expander Header-->
                                <!--Add more detail here for a fancier header.-->
                                <Border Grid.Column="1" Background="DarkRed">
                                    <ContentPresenter Margin="4" 
                                            ContentSource="Header"
                                            TextBlock.Foreground="White"
                                            RecognizesAccessKey="True"/>
                                </Border>

                            </Grid>
                        </Border>
                        <Border x:Name="Content"
                                    Grid.Row="1"
                                    BorderThickness="0"
                                    CornerRadius="0,0,2,2">
                            <ContentPresenter Margin="4" />
                        </Border>
                    </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsExpanded"
                                     Value="True">
                                <Setter TargetName="ContentRow"
                                        Property="Height"
                                        Value="{Binding DesiredSize, ElementName=Content}" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

enter image description here

我使用的样式具有切换按钮,几乎与“标题”文本和背景是一个单独的实体,但是如果您也想要这种外观,则很容易在切换按钮下方扩展标题的背景。一旦深入到ControlTemplate,就可以完全控制一切。