2017-06-30 32 views
1

WPF(C#)でVisual Studio 2013で作業していて、xmlからエキスパンダーを作成する次のコードがあります。現在のところ現在完全に動作していますが、すべてを展開してすべてのボタンを折りたたみたいのです。私は全面的に見て、解決策を見つけるように見えない。WPFでエキスパンダーを使用してすべて展開ボタンと折りたたみボタンを作成する

ここで、エクスパンダが作成されます。私は、アイテムのリストを繰り返し処理し、Property = "IsExpanded"をValue = "True"に変更して、すべてを展開する必要があることを知っています。

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

ここは、Xmlからの情報を持つDataTemplateを呼び出すコードです。

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

ここではすべて展開するために.csファイルで試したことがあります。

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

ご協力いただきありがとうございます。

答えて

2

xmldpUpgradeActionsはCollectionViewSourceですか?

コレクション内のどのクラスであれ、INotifyPropertyChangedを実装する必要があります。

はそれにその値が変化し、そのセッターにPropertyChangedを上げるIsExpanded性を付与し、テンプレートでExpander.IsExpandedにそれをバインドします

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

は、コレクションやセット内のすべての項目をループコマンドを書きますそれぞれにitem.IsExpanded = false;

+0

これは機能します。助けてくれてありがとう! –

+1

ダングエド最近、任務に関する質問を巻き起こしています。私は最近、これらの日々を守ることができません。+1 –

+0

@ChrisW。現時点での仕事の緊急性はほとんどありません。 –

関連する問題