更改ListView中不同类别项目的GroupStyle(启用/禁用)

3
我如何在运行时根据某些条件在ListView中的GroupStyles之间切换?例如,我需要对名称为null的GroupStyle头项使用默认样式,如果不是null,则使用自定义GroupStyle主题。我尝试过GroupStyleSelector,但它不起作用,因为它适用于多级分组,在我的情况下只有一级分组。
如果可以,请告诉我如何实现?
自定义GroupStyle:
    <Style x:Key="grouping"
           TargetType="{x:Type GroupStyle}">
        <Setter Property="ContainerStyle">
            <Setter.Value>
                <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="Margin"
                            Value="0,0,0,5" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type GroupItem}">
                                <Expander IsExpanded="False"
                                          BorderBrush="#FFA4B97F"
                                          BorderThickness="0,0,0,1">
                                    <Expander.Header>
                                        <DockPanel>
                                            <TextBlock FontWeight="Bold"
                                                       Text="{Binding Name}"
                                                       Margin="0"
                                                       Width="250" />
                                            <TextBlock FontWeight="Bold"
                                                       Text="{Binding Path=Items[0].StartTime, StringFormat=T}" />
                                        </DockPanel>
                                    </Expander.Header>
                                    <Expander.Content>
                                        <ItemsPresenter />
                                    </Expander.Content>
                                </Expander>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </Setter.Value>
        </Setter>                    
    </Style>

非常感谢。
真诚地, 弗拉德。
1个回答

4

好的,我找到了解决方法。基本上,我需要构建DataTrigger并在其中检查类别,如果匹配,则使用不同的GroupStyle。以下是示例:

  <ControlTemplate TargetType="{x:Type GroupItem}"
                   x:Key="defaultGroup">
       <ItemsPresenter />
  </ControlTemplate>

  <ListView.GroupStyle>
   <GroupStyle >                            
    <GroupStyle.ContainerStyle>
     <Style TargetType="{x:Type GroupItem}">
      <Setter Property="Margin"
        Value="0,0,0,5" />
      <Setter Property="Template">
       <Setter.Value>
        <ControlTemplate TargetType="{x:Type GroupItem}">
         <Expander IsExpanded="False"
             BorderBrush="Black"
             BorderThickness="3"
             Padding="5,1,1,5">
          <Expander.Header>
           <DockPanel>
            <TextBlock FontWeight="Bold"
                 Margin="0"
                 Width="250">
             <TextBlock.Text>
              <MultiBinding StringFormat="{}{0} ({1} jobs)">
               <Binding Path="Name" />
               <Binding Path="ItemCount" />
              </MultiBinding>
             </TextBlock.Text>
            </TextBlock>
            <TextBlock FontWeight="Bold"
                 Text="{Binding Path=Items[0].Category, StringFormat=T}" />
           </DockPanel>
          </Expander.Header>
          <Expander.Content>
           <ItemsPresenter />
          </Expander.Content>
         </Expander>
        </ControlTemplate>
       </Setter.Value>
      </Setter>
      <Style.Triggers>
       <DataTrigger Binding="{Binding Items[0].Category}"
           Value="ABC">
        <Setter Property="Template"
          Value="{StaticResource defaultGroup}" />
       </DataTrigger>
      </Style.Triggers>
     </Style>
    </GroupStyle.ContainerStyle>
   </GroupStyle>
  </ListView.GroupStyle>

如何在不使用DataTrigger的情况下实现相同的效果 - Tilak

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接