2017-06-30 2 views
1

Je travaille dans Visual Studio 2013 dans WPF (C#) et j'ai le code suivant pour créer un expandeur à partir de mon xml. Il fonctionne actuellement parfaitement, mais je veux inclure un tout développer et réduire tous les boutons. J'ai regardé partout et n'arrive pas à trouver une solution.Création d'un Expand All et Réduire tous les boutons avec Expander dans WPF

Ici, l'extension est créée. Je sais que je dois juste parcourir une liste d'éléments et changer la propriété = "IsExpanded" à Value = "True" pour créer un tout développer.

 <DataTemplate x:Key="dtListTemplate" > 
      <StackPanel> 
       <Expander LostFocus="CollapseExpander" ExpandDirection="Down" Width="Auto"> 
        <Expander.Style> 
         <Style TargetType="Expander"> 
          <Setter Property="IsExpanded" Value="False" /> 
          <Setter Property="Header" Value="{Binding [email protected]}" /> 
          <Setter Property="FontWeight" Value="Bold"/> 

          <Style.Triggers> 
           <DataTrigger Binding="{Binding IsExpanded,RelativeSource={RelativeSource Self}}" Value="True"> 
           </DataTrigger> 
          </Style.Triggers> 
         </Style> 
        </Expander.Style> 
        <ListBox Name="itemsList" 
         ItemsSource="{Binding XPath=UpgradeAction}" 
         ItemTemplate="{StaticResource dtListItemTemplate}" 
         SelectionChanged="listItems_SelectionChanged" 
         Style="{StaticResource styleListBoxUpgradeAction}" 
         ItemContainerStyle="{StaticResource styleListBoxItemUpgradeAction}"> 
        </ListBox> 
       </Expander> 
      </StackPanel> 
     </DataTemplate> 

Voici le code qui appelle le DataTemplate qui contient des informations provenant d'un fichier Xml.

<StackPanel> 
     <StackPanel Orientation="Horizontal" HorizontalAlignment="Right"> 
      <Border Grid.Column="0" Grid.Row="0" Width="790" Height="40" Padding="5" Background="#4E87D4"> 
       <Label VerticalAlignment="Center" FontSize="16" FontWeight="Bold" Foreground="White">Test</Label> 
      </Border> 
      <Button Name="ExpandBttn" Width="100" Height="40" FontSize="16" FontWeight="Bold" Content="Expand All" DataContext="{Binding}" Click="Expand_Button_Click"/> 
      <Button Name="ColapseBttn" Width="100" Height="40" FontSize="16" FontWeight="Bold" Content="Colapse All" DataContext="{Binding}" Click="Collapse_Button_Click"/> 
     </StackPanel> 
     <ListView Name="listItems" Grid.Column="0" Grid.Row="1" Background="Wheat" 
      ItemsSource="{Binding Source={StaticResource xmldpUpgradeActions}, XPath=ActionGroup}" 
      ItemTemplate="{StaticResource dtListTemplateRichards}" 
      SelectionChanged="listItems_SelectionChanged"> 
     </ListView> 
    </StackPanel> 

Voici ce que j'ai essayé dans le fichier .cs pour la partie "tout développer".

private void Expand_Button_Click(object sender, RoutedEventArgs e) 
    { 
     foreach(var item in listItems.Items) 
     { 
      var listBoxItem = listItems.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem; 
      var itemExpander = (Expander)GetExpander(listBoxItem); 
      if (itemExpander != null) 
       itemExpander.IsExpanded = true; 
     } 
    } 

    private static DependencyObject GetExpander(DependencyObject container) 
    { 
     if (container is Expander) return container; 

     for (var i = 0; i < VisualTreeHelper.GetChildrenCount(container); i++) 
     { 
      var child = VisualTreeHelper.GetChild(container, i); 

      var result = GetExpander(child); 
      if (result != null) 
      { 
       return result; 
      } 
     } 

     return null; 
    } 

Toute aide est grandement appréciée!

Répondre

2

Est-ce que xmldpUpgradeActions a CollectionViewSource? La classe

Quelle que soit la classe dans la collection, elle doit implémenter INotifyPropertyChanged.

Donnez-lui une propriété IsExpanded qui soulève PropertyChanged dans son setter lorsque sa valeur change, et liez-à Expander.IsExpanded dans le modèle:

<Expander 
    IsExpanded="{Binding IsExpanded}" 
    LostFocus="CollapseExpander" 
    ExpandDirection="Down" 
    Width="Auto"> 

Ecrire une commande qui boucle à travers tous les éléments de la collection et ensembles item.IsExpanded = false; sur chacun d'eux.

+0

Cela fonctionne. Merci pour l'aide! –

+1

Dang Ed vous rouler à travers des questions sur une mission ces derniers temps, je ne peux pas suivre ces jours haha ​​+1 –

+0

@ChrisW. Très peu de sentiment d'urgence au travail en ce moment, ha, ha. –