【问题标题】:Changing (Enable/Disable) GroupStyle in ListView for different category items在 ListView 中为不同的类别项目更改(启用/禁用)GroupStyle
【发布时间】:2008-10-21 17:30:17
【问题描述】:

如何在运行时根据某些条件在GroupStylesListView 之间切换?例如,我需要对 GroupStyle 标题名称为空的项目使用默认值,如果它不为空,则使用自定义 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>

非常感谢。

真诚地, 弗拉德。

【问题讨论】:

    标签: wpf listview listbox styles


    【解决方案1】:

    好的,

    我找到了解决方案。基本上我需要构建 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 的情况下达到同样的效果
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    • 2017-08-06
    • 2015-03-08
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 1970-01-01
    相关资源
    最近更新 更多