如果扩展一个扩展器,则多个扩展器必须折叠
我有 4 个扩展器控件。
当一个扩展器扩展时,如何使所有其他扩展器折叠/关闭?
I have 4 expander controls.
When one expander is expanded, how can I make all others collapse/close?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
尝试以下代码:
XAML:
Converter:
ViewModel:
Initialization
说明:
在 XAML 中,我们有 4 个扩展器。它们都从容器
StackPanel
通过DataContext
继承了ViewModel
(类型为ExpanderListViewModel
)。它们都绑定到 ViewModel 类上的单个属性。并在绑定中使用
ConverterParameter
为自己定义了唯一索引。每当您展开扩展器时,该索引都会保存在SelectedExpander
属性中。使用该索引,如果存储的索引与给定索引匹配,则Converter
返回true
;如果存储的索引不匹配,则返回false
。在
Converter
类的Convert
和ConvertBack
方法中放置断点,您将看到发生了什么。Try out following code:
XAML:
Converter:
ViewModel:
Initialization
Explanation:
In XAML we have 4 expanders. They all inherit a
ViewModel
(of typeExpanderListViewModel
) from containerStackPanel
throughDataContext
.They all bind to single property on
ViewModel
class. And have defined a unique index for themselves usingConverterParameter
in binding. That index gets saved inSelectedExpander
property whenever you expand an expander. And using that index, theConverter
returnstrue
if the stored index matches with given index andfalse
if stored index does not match.Put a breakpoint in
Convert
andConvertBack
methods ofConverter
class and you will see what is going on.我就是这样做的:
1)添加了一个 StackPanel 并且必须添加一个名称标签属性(因为这是主面板)。
2) 根据需要添加任意数量的扩展器(如果需要,可添加 1 到 100 个),每个扩展器必须具有:-
(注意所有措辞 100% 相同)。
3)没有其他细节需要匹配(不需要身高的名字等)。
XML:
4) 要控制名为“StackPanel1”的StackPanel 上所有“Expander”的打开/关闭,您只需添加以下代码一次。
VB 代码隐藏:
5)现在您可以更改/添加内容、按钮、文本框等。您只需要不要更改 2 件事 1、“StackPanel 名称”2、“Expander Expanded”,而无需更新其他代码隐藏内容不会起作用。
希望这些信息对您有帮助。
发生什么事了?
1) 所有面板都是父面板,该面板上的所有控件都是子面板,
2) 所有控件都是父面板的子面板。
3) 一个类一次处理一个调用。
4)班级与孩子打交道。
6) 班级移至下一个孩子。
7) 一旦所有孩子都被问到,就停止。
所以伪代码是这样的:
1) 监听一个孩子的名字 x
2) 询问孩子父母列表中的每个孩子
3) 如果孩子没有打电话,那么
4) 孩子展开是假的
5) 结束询问孩子
6) 移动到下一个孩子并再次询问
7) 直到所有孩子都被问过
this is how I did it:
1) added a StackPanel and MUST add a name tag attribute (as this is the master).
2) add as many Expanders as you need (1 to 100's if needed) each MUST have:-
added (notice all have 100% the same wording).
3) no other details need to match on each ( no height's names etc.. needed).
Xaml:
4) To control the open/close of all "Expanders" on the named "StackPanel1" StackPanel you only need to add the below code once.
VB code-behind:
5)Now you can change/add what content, button's, textbox's etc.. you need just do not change 2 things 1, "StackPanel Name" 2, "Expander Expanded" without updating the code-behind else things will not work.
Hope this information is helpful to you.
What's happening?
1) All panels are parents and all controls on that panel are children,
2) All controls are children of a parent panel.
3) A class deals with one call at a time.
4) The class deals with child.
6) The class move to next child.
7) Stops once all children have been asked.
So the pseudo code is like this:
1) Listen for a child’s named x
2) Ask each child in parents list of children
3) If child is not calling then
4) Child is expanded is false
5) End asking that child
6) Move to next child and ask again
7) Until all children have been asked
只需设置“失去焦点”似乎是最简单的方法。
XAML:
VB:
Just setting the Lost focus seems to be the easiest way to do this.
Xaml:
VB:
使用 MVVM 并将 IsExpanded 属性绑定到视图模型上的布尔标志。当其中一个更新为
true
时,将所有其他设置为false
。Use MVVM and bind the IsExpanded property to a boolean flag on your view models. When one is updated to
true
, set all the others tofalse
.@wassim-azirar 询问已接受的答案:
我在 ViewModel 中添加:
因为事实上,“1”与 XAML 中的“1”不是同一个对象,所以这是行不通的,所以我改变了 decyclone 的答案,如下所示:
decyclone 的答案对我来说非常有帮助- 谢谢。
所以我想分享一下我的经验,如果有人需要的话。
@wassim-azirar asked to the accepted answer:
I added in the ViewModel:
Because of the fact, that the "1" is not the same object as the "1" in XAML this will not work, so I changed decyclone's answer like this:
The answer of decyclone was very helpful for me - Thanks.
So I would like to share my experience if someone needs it.
尝试 WPF 工具包 - 2010 年 2 月发布
http://www.dotnetspark.com/kb/1931-accordion-wpf-toolkit-tutorial.aspx
示例代码:
Try the Accordion control from WPF Toolkit - February 2010 Release
http://www.dotnetspark.com/kb/1931-accordion-wpf-toolkit-tutorial.aspx
Sample code:
我也需要这个,但在我看来,所有答案都太费力了。
我是这样做的:
每人有 2 个事件
那个它。
I also needed this, but all answers was too much work IMO.
Here is how I did it:
each one got 2 events
that it.