作者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://www.ptt.cc/bbs/C_Sharp/M.1490337699.A.B18.html
※ 編輯: b26168 (220.128.199.139), 03/24/2017 14:59:32
推 name2name2: 你可以試試查看方便暫時給ListView一個名字 03/29 00:55
→ name2name2: 然後跑過你給初始isSelected值部分後 03/29 00:56
→ name2name2: 假設你給名字是lview 03/29 00:57
→ name2name2: 可在debug模式下查看 lsview.SelectedItems 03/29 00:58
→ name2name2: 有沒有包含你設成true的那些物件? 03/29 00:59
→ name2name2: 或者說 你可以說說你怎麼給初始值的嗎 03/29 01:00
→ name2name2: 是在ViewModel 寫一個集合 然後在ListView寫Binding 03/29 01:01
→ name2name2: ItemSource到那個集合 或者是直接用Code賦值ItemSouce 03/29 01:02
→ name2name2: 03/29 01:03
→ name2name2: 另外假設一個group裡有多筆資料 那只有其中一筆被 03/29 01:04
→ name2name2: Select讓IsSelected變true的話 其他多筆還是false 03/29 01:04
→ name2name2: 同層ListViewItem有的true有的false 猜應難直接Bindin 03/29 01:06