我想向CollectionViewGroup
所以我参考了这篇文章: Custom CollectionViewType does not support multiple selection using shift key
单击按钮添加新项目
collection add success(default count=9, add one, total count=10),
但listbox
未显示新项目组。
也许有些事情难以实施。
请帮帮我,或者给我关键字google。
谢谢。
[Window1.xaml]
<Window.Resources>
<CollectionViewSource x:Key="EmployeesSource" Source="{Binding Employees}" CollectionViewType="local:EmployeeCollectionView">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="HireDate"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
<DataTemplate x:Key="EmployeeTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FirstName}" />
<TextBlock Text="{Binding LastName}" />
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0">
<Button Name="addItem" Width="50" Height="50" Content="AddItem" HorizontalAlignment="Left" Click="addItem_Click"/>
</StackPanel>
<ListBox Grid.Row="1" ItemsSource="{Binding Source={StaticResource EmployeesSource}}"
ItemTemplate="{StaticResource EmployeeTemplate}"
SelectionMode="Extended">
<ListBox.GroupStyle>
<x:Static Member="GroupStyle.Default"/>
</ListBox.GroupStyle>
</ListBox>
</Grid>
[Window1.xaml.cs]
public partial class Window1 : Window
{
public ObservableCollection<Employee> Employees { get; set; }
public Window1()
{
InitializeComponent();
Employees = new ObservableCollection<Employee>
{
new Employee { FirstName = "FirstName1", LastName = "LastName1", HireDate = new DateTime(1970, 1, 1) },
new Employee { FirstName = "FirstName2", LastName = "LastName2", HireDate = new DateTime(1970, 1, 1) },
new Employee { FirstName = "FirstName3", LastName = "LastName3", HireDate = new DateTime(1980, 1, 1) },
new Employee { FirstName = "FirstName4", LastName = "LastName4", HireDate = new DateTime(1980, 1, 1) },
new Employee { FirstName = "FirstName5", LastName = "LastName5", HireDate = new DateTime(1980, 1, 1) },
new Employee { FirstName = "FirstName6", LastName = "LastName6", HireDate = new DateTime(1990, 1, 1) },
new Employee { FirstName = "FirstName7", LastName = "LastName7", HireDate = new DateTime(1990, 1, 1) },
new Employee { FirstName = "FirstName8", LastName = "LastName8", HireDate = new DateTime(1990, 1, 1) },
new Employee { FirstName = "FirstName9", LastName = "LastName9", HireDate = new DateTime(1990, 1, 1) },
};
DataContext = this;
}
private void addItem_Click(object sender, RoutedEventArgs e)
{
//Employees add suceess(total count=10), but ui does not add the item(1983,1,1)
Employees.Add(new Employee {
FirstName = "FirstName10",
LastName = "LastName10",
HireDate = new DateTime(1983, 1, 1)
});
}
}
class EmployeeCollectionView : ListCollectionView
{
private readonly IList<EmployeeCollectionViewGroup> _groups;
public override ReadOnlyObservableCollection<object> Groups
{
get { return new ReadOnlyObservableCollection<object>(new ObservableCollection<object>(_groups)); }
}
public EmployeeCollectionView(IList list)
: base(list)
{
_groups = list
.OfType<Employee>()
.GroupBy(x => x.HireDate)
.Select(x => new EmployeeCollectionViewGroup(x.Key, x))
.ToList();
}
}
class EmployeeCollectionViewGroup : CollectionViewGroup
{
public EmployeeCollectionViewGroup(object name, IEnumerable<object> protectedItems)
: base(name)
{
foreach (var protectedItem in protectedItems)
ProtectedItems.Add(protectedItem);
}
public override bool IsBottomLevel
{
get { return true; }
}
//i want to add a new property
public bool IsDeleting { get; set; }
}
public class Employee : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string m_FirstName;
public string FirstName
{
get { return m_FirstName; }
set
{
m_FirstName = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("FirstName"));
}
}
private string m_LastName;
public string LastName
{
get { return m_LastName; }
set
{
m_LastName = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("LastName"));
}
}
private DateTime m_HireDate;
public DateTime HireDate
{
get { return m_HireDate; }
set
{
m_HireDate = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("HireDate"));
}
}
}
答案 0 :(得分:-1)
您的代码已将ListBox ItemSource绑定到类Groups
的属性EmployeeCollectionView
,其类型为ReadOnlyObservableCollection<object>
,它是一个只读属性。添加新员工时,无法通知UI进行更新。
您可以尝试将ListBox标记更改为:
<ListBox Grid.Row="1" ItemsSource="{Binding Employees}"
ItemTemplate="{StaticResource EmployeeTemplate}"
SelectionMode="Extended">
<ListBox.GroupStyle>
<x:Static Member="GroupStyle.Default"/>
</ListBox.GroupStyle>
</ListBox>
直接将ItemSource绑定到Employees。 希望能帮到你:)。