【问题标题】:Creating an Expand All and Collapse All Buttons with Expander in WPF在 WPF 中使用扩展器创建全部展开和折叠所有按钮
【发布时间】:2017-06-30 18:52:35
【问题描述】:

我在 WPF (C#) 中的 Visual Studio 2013 中工作,我有以下代码从我的 xml 创建扩展器。它目前正在完美运行,但我想包含一个全部展开和折叠所有按钮。我找遍了,似乎找不到解决办法。

这里是创建扩展器的地方。我知道我只需要遍历项目列表并将 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 XPath=@Name}" />
                            <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>

这是调用 DataTemplate 的代码,该数据模板包含来自 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>

这是我在 .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;
  }

非常感谢任何帮助!

【问题讨论】:

    标签: c# wpf xaml visual-studio-2013 expander


    【解决方案1】:

    xmldpUpgradeActions 是 CollectionViewSource 吗?

    无论集合中有什么类,它都应该实现INotifyPropertyChanged

    给它一个 IsExpanded 属性,当它的值发生变化时,它会在其 setter 中引发 PropertyChanged,并将其绑定到模板中的 Expander.IsExpanded

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

    编写一个循环遍历集合中所有项目的命令,并在每个项目上设置item.IsExpanded = false;

    【讨论】:

    • Dang Ed,你最近在处理任务中的问题,这些天我跟不上哈哈+1
    • @ChrisW。目前工作的紧迫感很少,哈哈。
    猜你喜欢
    • 1970-01-01
    • 2015-03-29
    • 1970-01-01
    • 1970-01-01
    • 2016-05-31
    • 1970-01-01
    • 2010-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多