如何根据 WPF 中的组名称折叠/展开多个 Expander 控件?
我想要实现的目标:折叠或展开组样式中共享相同组名称的所有扩展器。
我们有一个 ListBox 和另一个嵌套在其中的 ListBox 以显示子项。子项 ItemsSource 绑定到带有附加组描述的 CollectionView。
组项目模板非常简单:
<Expander IsExpanded="{Binding Path=WHAT_TO_DO, Mode=TwoWay}">
<Expander.Header>
<TextBlock Text="{Binding Name}" />
</Expander.Header>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" SharedSizeGroup="{Binding Name, Converter={StaticResource GroupNameToUniqueNameConverter}}" />
</Grid.RowDefinitions>
<ItemsPresenter/>
</Grid>
</Expander>
What I want to achieve: To collapse or expand all Expanders that share the same group name within a group style.
We have a ListBox and another ListBox nested inside it to display child items. The child items ItemsSource is bound to a CollectionView with Group descriptions attached.
The group item template is quite simple:
<Expander IsExpanded="{Binding Path=WHAT_TO_DO, Mode=TwoWay}">
<Expander.Header>
<TextBlock Text="{Binding Name}" />
</Expander.Header>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" SharedSizeGroup="{Binding Name, Converter={StaticResource GroupNameToUniqueNameConverter}}" />
</Grid.RowDefinitions>
<ItemsPresenter/>
</Grid>
</Expander>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为这不是那么容易就能做到的。这里可能有两个选项
1) 手动 - 绑定您需要的每个扩展器,例如。组中的第一项或任何其他合适的项目。
2)更自动的方法可能需要编写一个类,该类具有保存每个组状态的字典。这里的问题是控制器需要知道扩展器的哪个实例正在执行绑定或它位于哪个组中。我在这里看到的一种解决方案是编写一个自定义转换器,该转换器具有带字典的类的静态实例并使用转换器参数来传递组/引用(这里可以使用绑定,所以在xaml中这是纯Ctrl+C、Ctrl+V操作)
I don't think this can be done so easily. There are probably two options here
1) Manually - bind every expander that you need to eg. the first item in the group or anything else that is appropriate.
2) More automatic way would probably require writing a class that has a dictionary that holds states for each group. The problem here is that the controller needs to know which instance of expander is doing the binding or which group it is in. One solution that I see here is writing a custom converter that has a static instance of the class with dictionary and using the converter parameter to pass the group/reference (you can use binding here, so in xaml this is pure Ctrl+C, Ctrl+V operation)
您可以在代码后面迭代所有列表框项目。对于每个列表框项目,您应该查看它是否包含扩展器。如果是这样,您只需展开或折叠它即可。请参阅此链接了解如何迭代项目并查找特定控件。
有没有办法在列表框项目中进行迭代模板?
You can in code behind iterate over all listbox items. For each listbox item you should see if it contains an expander. If it does you just expand or collapse it. See this link for how to iterate over items and finding a specific control.
Is there a way to iterate in a ListBox Items templates?
创建您自己的控件 - GroupExpander - 从 Expander 对其进行子类化
使用以下辅助方法在视觉层次结构中查找任何父级:
编写查找 GroupExpander 级别的逻辑
现在通过查找父级扩展器并计算您可以从中获得想法来 下面的代码:
Create your own control - GroupExpander - subclass it from Expander
Use the following helper method to find any parent in the visual hierarchy:
now write the logic of finding Level of GroupExpander by finding parent expander and calculating which expander this is
you can get the idea from the following code: