[問題] wpf Expander 當 item 被選取自動 expand

看板C_Sharp (C#)作者 (b26168)時間8年前 (2017/03/24 14:41), 8年前編輯推噓1(1011)
留言12則, 1人參與, 最新討論串1/1
我做了一個 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

03/29 00:55, , 1F
你可以試試查看方便暫時給ListView一個名字
03/29 00:55, 1F

03/29 00:56, , 2F
然後跑過你給初始isSelected值部分後
03/29 00:56, 2F

03/29 00:57, , 3F
假設你給名字是lview
03/29 00:57, 3F

03/29 00:58, , 4F
可在debug模式下查看 lsview.SelectedItems
03/29 00:58, 4F

03/29 00:59, , 5F
有沒有包含你設成true的那些物件?
03/29 00:59, 5F

03/29 01:00, , 6F
或者說 你可以說說你怎麼給初始值的嗎
03/29 01:00, 6F

03/29 01:01, , 7F
是在ViewModel 寫一個集合 然後在ListView寫Binding
03/29 01:01, 7F

03/29 01:02, , 8F
ItemSource到那個集合 或者是直接用Code賦值ItemSouce
03/29 01:02, 8F

03/29 01:03, , 9F
03/29 01:03, 9F

03/29 01:04, , 10F
另外假設一個group裡有多筆資料 那只有其中一筆被
03/29 01:04, 10F

03/29 01:04, , 11F
Select讓IsSelected變true的話 其他多筆還是false
03/29 01:04, 11F

03/29 01:06, , 12F
同層ListViewItem有的true有的false 猜應難直接Bindin
03/29 01:06, 12F
文章代碼(AID): #1OrB-ZiO (C_Sharp)
文章代碼(AID): #1OrB-ZiO (C_Sharp)