我有一个扩展器列表,我想用全局切换按钮控制其展开状态(IsExpanded),该按钮应该在展开/折叠状态之间切换。
到目前为止,我得到的解决方案是通过将扩展器的 IsExpanded 状态绑定(bind)到切换按钮的 IsChecked 状态来实现的。只要我不手动不切换扩展器,这就会起作用。一旦我这样做了,那些特定的扩展器就不尊重绑定(bind)(切换按钮的 IsChecked 状态)。
知道为什么吗? XAML 中是否有一个干净的解决方案?
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<StackPanel>
<ToggleButton Name="ExpandAll">Toggle</ToggleButton>
<Expander IsExpanded="{Binding ElementName=ExpandAll,Path=IsChecked, Mode=OneWay}">
Hai
</Expander>
<Expander IsExpanded="{Binding ElementName=ExpandAll,Path=IsChecked, Mode=OneWay}">
Hello
</Expander>
<Expander IsExpanded="{Binding ElementName=ExpandAll,Path=IsChecked, Mode=OneWay}">
Weird
</Expander>
</StackPanel>
</Grid>
</Page>
最佳答案
我知道这篇文章很老了。只是为遇到的其他人发布此信息。下面的代码对我有用。
<Expander IsExpanded="{Binding ElementName=ExpandAll, Path=IsChecked, UpdateSourceTrigger=Explicit}">
</Expander>
这在动态生成扩展器时有效,例如在 DataGrid.RowDetailsTemplate
内。
关于wpf - 展开/折叠所有扩展器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4132768/