[问题] wpf Expander 当 item 被选取自动 expand

楼主: b26168 (b26168)   2017-03-24 14:41:36
我做了一个 ListView 跟 DataGridView
都完成了 Grouping 的动作
但是当我动态去改变 selectd 的时候(也静态给定初始值)
group 的 expander 没办法自动地展开
部分的 xaml design 如下:
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander IsExpanded="{Binding
Mode=TwoWay, Path=IsSelected, RelativeSource={RelativeSource
AncestorType=ListViewItem, Mode=FindAncestor}}">
<Expander.Header>
<StackPanel
Orientation="Horizontal">
<TextBlock Text="{Binding
Name}" FontWeight="Bold" Foreground="Gray" FontSize="16"
VerticalAlignment="Bottom"/>
<TextBlock Text="{Binding
ItemCount}" FontSize="22" Foreground="Green" FontWeight="Bold"
FontStyle="Italic" Margin="10,0,0,0" VerticalAlignment="Bottom" />
<TextBlock Text="
item(s)" FontSize="22" Foreground="Silver" FontStyle="Italic"
VerticalAlignment="Bottom" />
</StackPanel>
</Expander.Header>
<ItemsPresenter/>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
主要的应该就是
<Expander IsExpanded="{Binding Mode=TwoWay, Path=IsSelected,
RelativeSource={RelativeSource AncestorType=ListViewItem,
Mode=FindAncestor}}">
将 AncestorType 设为 ListViewItem 是否正确??
还是上面还缺了什么宣告?
作者: name2name2 (yang~hi)   2017-03-29 00:55:00
你可以试试查看方便暂时给ListView一个名字然后跑过你给初始isSelected值部分后假设你给名字是lview可在debug模式下查看 lsview.SelectedItems有没有包含你设成true的那些物件?或者说 你可以说说你怎么给初始值的吗是在ViewModel 写一个集合 然后在ListView写BindingItemSource到那个集合 或者是直接用Code赋值ItemSouce另外假设一个group里有多笔资料 那只有其中一笔被Select让IsSelected变true的话 其他多笔还是false同层ListViewItem有的true有的false 猜应难直接Bindin

Links booklink

Contact Us: admin [ a t ] ucptt.com