wpf - Expander IsExpanded 绑定(bind)到选定的列表框

标签 wpf binding listbox expander

所以我有一个Listbox,其项目包含一个Expander。我想要做的是将 IsExpanded 属性绑定(bind)到父 ListBoxItemIsSelected 属性。整个目的是,如果他们选择了不同的列表框项目,它将折叠当前所选项目并展开新所选项目。

更新(代码):

<ListBox SelectionMode="Single">
    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            <Setter Property="Padding" Value="0" />
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBox.ItemTemplate>
        <DataTemplate>
             <Boreder BorderThickness="0,0,0,1"
                      BorderBrush="Black">
                 <Expander IsExpanded={Binding RelativeSource={RelativeSource AncestorType=ListBoxItem},
                                               Path=IsSelected} />
             </Border>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

最佳答案

我正在寻找的属性/绑定(bind)是IsExpanded="{Binding Mode=TwoWay, Path=IsSelected,relativeSource={RelativeSource AncestorType=ListBoxItem, Mode=FindAncestor}}"

关于wpf - Expander IsExpanded 绑定(bind)到选定的列表框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34861001/

相关文章:

vb.net - 从列表框中删除多个项目或行 VB.NET

wpf - 捕获线程、任务、计时器、ThreadPool 线程和后台工作线程上引发的 UnhandledExceptions

wpf - MenuItem 不会在其菜单中垂直拉伸(stretch)

wpf - TreeView 与 MVVM 中的动态 ContextMenu

wpf - 将 LocBaml 和资源合并到单个卫星组件中

c# - 具有行和列标题WPF的ItemsControl

c# - 以枚举为键绑定(bind)到字典中的值

c# - Win 表单中的单变量绑定(bind) : What am I doing wrong?

c# - 如何在数据库中 nvarchar() 列的不同行中更新列表框的多个选择?

listbox - 避免列表框可滚动