作者b26168 (b26168)
看板C_Sharp
标题[问题] wpf Expander 当 item 被选取自动 expend
时间Fri Mar 24 14:41:36 2017
我做了一个 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 是否正确??
还是上面还缺了甚麽宣告?
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 220.128.199.139
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_Sharp/M.1490337699.A.B18.html
※ 编辑: b26168 (220.128.199.139), 03/24/2017 14:59:32
1F:推 name2name2: 你可以试试查看方便暂时给ListView一个名字 03/29 00:55
2F:→ name2name2: 然後跑过你给初始isSelected值部分後 03/29 00:56
3F:→ name2name2: 假设你给名字是lview 03/29 00:57
4F:→ name2name2: 可在debug模式下查看 lsview.SelectedItems 03/29 00:58
5F:→ name2name2: 有没有包含你设成true的那些物件? 03/29 00:59
6F:→ name2name2: 或者说 你可以说说你怎麽给初始值的吗 03/29 01:00
7F:→ name2name2: 是在ViewModel 写一个集合 然後在ListView写Binding 03/29 01:01
8F:→ name2name2: ItemSource到那个集合 或者是直接用Code赋值ItemSouce 03/29 01:02
9F:→ name2name2: 03/29 01:03
10F:→ name2name2: 另外假设一个group里有多笔资料 那只有其中一笔被 03/29 01:04
11F:→ name2name2: Select让IsSelected变true的话 其他多笔还是false 03/29 01:04
12F:→ name2name2: 同层ListViewItem有的true有的false 猜应难直接Bindin 03/29 01:06