Modifica (Abilita / Disabilita) GroupStyle in ListView per diversi elementi di categoria

StackOverflow https://stackoverflow.com/questions/222729

  •  03-07-2019
  •  | 
  •  

Domanda

Come posso passare da GroupStyles per un ListView in base ad alcune condizioni in fase di esecuzione? Ad esempio, devo usare Predefinito per gli articoli che hanno GroupStyle Nome dell'intestazione null e, se non è null, utilizzare il tema GroupStyle personalizzato? Ho provato GroupStyleSelector e non funziona, perché funziona per il raggruppamento a più livelli e nel mio caso ho un solo raggruppamento a livello.

Se sì, come?

GroupStyle personalizzato :

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

Grazie mille.

Cordiali saluti, Vlad.

È stato utile?

Soluzione

D'accordo,

Ho trovato una soluzione per questo. Fondamentalmente avevo bisogno di costruire DataTrigger e verificare la categoria in esso, e se corrisponde, utilizzare GroupStyle diverso. Ecco l'esempio:

  <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>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top