[問題] 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