【问题标题】:Collapse expander inside ItemsControl WPFItemsControl WPF 内的折叠扩展器
【发布时间】:2014-09-19 06:05:16
【问题描述】:

我在 ItemsControl 中显示扩展器项目列表并将项目列表绑定到它。 我遵循的 xaml 结构是

    <ItemsControl ItemsSource="{Binding}" >            
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Expander ExpandDirection="Down" Style="{StaticResource ResourceKey=ExpanderItemStyle}" >
                <Expander.Header>
                    <BulletDecorator>
                    //Expander header by default its collapsed 
                    <Label Style="{StaticResource ResourceKey=ChapterHeadStyle}"  Content="{Binding name}"></Label>                                         
                    </BulletDecorator>
                </Expander.Header>
                <StackPanel> // Expander body want to collapse it when some some other items of ItemsControl is expanded 
                </StackPanel>
            </Expander>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

当 ItemsControl 中的一些其他项目被选中时,我可以使扩展器内容自动折叠

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    您可以将ItemsControl 替换为ListBox 并将ExpanderIsExpanded 属性绑定到ListBoxItemIsSelected 属性。 (见http://social.msdn.microsoft.com/Forums/vstudio/en-US/a2988ae8-e7b8-4a62-a34f-b851aaf13886/windows-presentation-foundation-faq?forum=wpf#expander_list

    您还可以将IsExpanded 绑定到某个视图模型属性并从那里获取它。通过 wpf 行为可以实现相同的结果。

    回答您的问题:不,没有“自动”方式来折叠其他扩展器。

    【讨论】:

    • IsExpanded="{Binding Mode=TwoWay, Path=IsSelected, RelativeSource={RelativeSource AncestorType=ListBoxItem, Mode=FindAncestor}}" 这种方式对我有用 谢谢帮助:)
    猜你喜欢
    • 1970-01-01
    • 2015-03-29
    • 2011-11-30
    • 1970-01-01
    • 1970-01-01
    • 2019-06-14
    • 1970-01-01
    • 1970-01-01
    • 2017-03-06
    相关资源
    最近更新 更多