在WPF ListBox-GroupItem中对ListItem进行排序和分组折叠和展开
我对列表框项进行排序和分组,我在此目的使用CollectioView。
从视图模型类I绑定ListBox ItemSource属性的集合,这是它。
public BindableCollection<UserInfo> Friends
{
get { return _friends; }
set
{
_friends = value;
NotifyOfPropertyChange(() => Friends);
}
}
ListBox项是UserInfo的类型。
当我初始化ListBox时,我使用此方法对项目进行排序和分组。
private ICollectionView _currentView;
//...
private void SortContactList()
{
_currentView = CollectionViewSource.GetDefaultView(Friends);
_currentView.GroupDescriptions.Add(new PropertyGroupDescription("TextStatus"));
_currentView.SortDescriptions.Add(new SortDescription("TextStatus", ListSortDirection.Ascending));
_currentView.SortDescriptions.Add(new SortDescription("Nick", ListSortDirection.Ascending));
}
TextStatus和Nick是userInfo类的属性。
我在Listbox GroupStyle中使用。这是它:
<Style x:Key="MessengerView_ToogleBtn" TargetType="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Image x:Name="img" Source="/images/icons/Collapse.png" />
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="False">
<Setter TargetName="img" Property="Source" Value="/images/icons/Expand.png" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding Path=IsBottomLevel}" Value="True">
<Setter TargetName="gridTemplate" Property="Grid.Background" Value="White" />
</DataTrigger>
</ControlTemplate.Triggers>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid Background="Black"
x:Name="gridTemplate"
Height="26"
VerticalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="45" />
</Grid.ColumnDefinitions>
<ToggleButton x:Name="btnShowHide"
IsChecked="True"
Style="{StaticResource MessengerView_ToogleBtn}"/>
<TextBlock Style="{StaticResource MessengerView_LbGroupHeader_TextBlock}"
Text="{Binding Path=Name}"
Grid.Column="1"/>
<TextBlock TextAlignment="Left" Style="{StaticResource MessengerView_LbGroupHeader_TextBlock}"
Grid.Column="2"
Text="{Binding Path=ItemCount}"/>
</Grid>
<ItemsPresenter Visibility="{Binding ElementName=btnShowHide, Path=IsChecked,
Converter={StaticResource booleanToVisibilityConverter}}"
Margin="3,3,3,3"
Grid.Row="1" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
如果我运行应用程序,它会看到这张照片。
1。
我编辑ListBox的源属性,(添加,删除,更新),编辑后的列表框后我在CollectionView上调用了Refresh方法。
_currentView.Refresh();
问题是GroupItem是崩溃的,我在所有GroupItem上调用Refresh方法都被展开了。
例如。
GroupItem 1崩溃。
GroupItem 2被推出。
GroupItem 3崩溃了。
在调用Refresh ListBox之前看起来像这张图片:
我在CollectionView上调用了Refresh方法,并扩展了所有GroupItems。我想保持原始状态,我该如何实现?
调用后刷新Lisbox看起来像在顶部的第一张图片。
没有找到相关结果
已邀请:
1 个回复
秃拳割
它们与控件绑定在这里:
和这里:
如果在外部字典中的某处为GroupItem定义Template,则必须使用
才能访问代码隐藏。