如何使CollectionView分组尊重数据模板设置

时间:2012-04-16 07:17:50

标签: wpf

我的ListBox有组样式:

<GroupStyle HidesIfEmpty="True" x:Key="GroupStyle">
            <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="Margin" Value="0,0,0,5"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type GroupItem}">
                                <Expander IsExpanded="True" BorderBrush="#FFA4B97F" BorderThickness="0,0,0,1">
                                    <Expander.Header>
                                        <DockPanel Background="LightSkyBlue" 
                                                           Width="{Binding RelativeSource={RelativeSource  Mode=FindAncestor,  AncestorType={x:Type Expander}},
                                                                           Path=ActualWidth}">
                                            <TextBlock FontWeight="Bold" Text="{Binding Path=Name}" Margin="5,0,0,0" Width="100"/>
                                            <TextBlock FontWeight="Bold" Text="{Binding Path=ItemCount}"/>
                                        </DockPanel>
                                    </Expander.Header>
                                    <Expander.Content>
                                        <ItemsPresenter/>
                                    </Expander.Content>
                                </Expander>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </GroupStyle.ContainerStyle>
        </GroupStyle>

绑定集合中的任何更改都会导致扩展程序重新打开。 有没有办法记住用户偏好并让扩展器折叠?

2 个答案:

答案 0 :(得分:1)

问题是你正在使用绑定集合做什么。 我同意Nikolay的观点,最好的选择是让你的viewmodel有一个属性,你将扩展器绑定到。

至于我在开头问过的问题:你是否对CollectionView有所了解? 刷新会导致重新创建UI。 (意思是,它就像在ObservableCollection上重置一样。它告诉UI创建用户控件并再次加载模板的XAML,这对性能有害)。

答案 1 :(得分:0)

我可以建议一个简单的黑客 - 只需将IsExpanded属性添加到您的模型类并将Expander.IsExpanded绑定到它。