C# WPF - 在 DataView 中添加新项目后显示在 ListBox 中排序的项目
C# WPF - Show items sorted in ListBox after adding new item in DataView
添加新项目时如何对DataView进行排序?
我有包含 ListBox 控件的 UserControl,其中 ItemsSource 设置为 DataView。当我将新项目添加到 DataView 时,项目始终显示在列表框中的最后一个。
对 DataView 进行排序并在 ListBox 中显示新项目的最佳方式是什么?
DataView ListBoxItems = new DataView();
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
DataTable dt = ..."SELECT * FROM table" //populated from database
ListBoxItems = dt.DefaultView;
ListBoxItems.Sort = "Col1 DESC, Col2 ASC";
ListBox.ItemsSource = ListBoxItems;
}
//Adding new item into DataView
DataRowView row = ListBoxItems.AddNew();
row["Col1"] = value1;
row["Col2"] = value2;
ListBoxItems.EndInit();
ListBox.SelectedItem = row;
ListBox.ScrollIntoView(row);
编辑:
我尝试了 Max Play 提供的 link 的解决方案,但没有成功。
添加新项目的更新代码:
//Adding new item into DataView
DataRowView row = ListBoxItems.AddNew();
row["Col1"] = value1;
row["Col2"] = value2;
ListBoxItems.EndInit();
ListBox.Items.SortDescriptions.Clear();
ListBox.Items.SortDescriptions.Add(new System.ComponentModel.SortDescription("Col1", System.ComponentModel.ListSortDirection.Descending));
ListBox.Items.SortDescriptions.Add(new System.ComponentModel.SortDescription("Col2", System.ComponentModel.ListSortDirection.Ascending));
ListBox.SelectedItem = row;
ListBox.ScrollIntoView(row);
新项目总是在 ListBox 中最后显示而不是最先显示(基于排序选项)。
已找到解决方案,现在按预期运行。
DataView ListBoxItems = new DataView();
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
DataTable dt = ..."SELECT * FROM table" //populated from database
ListBoxItems = dt.DefaultView;
ListBoxItems.Sort = "Col1 DESC, Col2 ASC";
ListBox.ItemsSource = ListBoxItems;
}
//Adding new item
BindingListCollectionView cv = (BindingListCollectionView)CollectionViewSource.GetDefaultView(ListBox.ItemsSource);
DataRowView row = (DataRowView) cv.AddNew();
row["Col1"] = value1;
row["Col2"] = value2;
cv.CommitNew();
cv.SortDescriptions.Clear();
cv.SortDescriptions.Add(new SortDescription("Col1", ListSortDirection.Descending));
cv.SortDescriptions.Add(new SortDescription("Col2", ListSortDirection.Ascending));
ListBox.SelectedItem = row;
ListBox.ScrollIntoView(row);
添加新项目时如何对DataView进行排序? 我有包含 ListBox 控件的 UserControl,其中 ItemsSource 设置为 DataView。当我将新项目添加到 DataView 时,项目始终显示在列表框中的最后一个。 对 DataView 进行排序并在 ListBox 中显示新项目的最佳方式是什么?
DataView ListBoxItems = new DataView();
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
DataTable dt = ..."SELECT * FROM table" //populated from database
ListBoxItems = dt.DefaultView;
ListBoxItems.Sort = "Col1 DESC, Col2 ASC";
ListBox.ItemsSource = ListBoxItems;
}
//Adding new item into DataView
DataRowView row = ListBoxItems.AddNew();
row["Col1"] = value1;
row["Col2"] = value2;
ListBoxItems.EndInit();
ListBox.SelectedItem = row;
ListBox.ScrollIntoView(row);
编辑: 我尝试了 Max Play 提供的 link 的解决方案,但没有成功。 添加新项目的更新代码:
//Adding new item into DataView
DataRowView row = ListBoxItems.AddNew();
row["Col1"] = value1;
row["Col2"] = value2;
ListBoxItems.EndInit();
ListBox.Items.SortDescriptions.Clear();
ListBox.Items.SortDescriptions.Add(new System.ComponentModel.SortDescription("Col1", System.ComponentModel.ListSortDirection.Descending));
ListBox.Items.SortDescriptions.Add(new System.ComponentModel.SortDescription("Col2", System.ComponentModel.ListSortDirection.Ascending));
ListBox.SelectedItem = row;
ListBox.ScrollIntoView(row);
新项目总是在 ListBox 中最后显示而不是最先显示(基于排序选项)。
已找到解决方案,现在按预期运行。
DataView ListBoxItems = new DataView();
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
DataTable dt = ..."SELECT * FROM table" //populated from database
ListBoxItems = dt.DefaultView;
ListBoxItems.Sort = "Col1 DESC, Col2 ASC";
ListBox.ItemsSource = ListBoxItems;
}
//Adding new item
BindingListCollectionView cv = (BindingListCollectionView)CollectionViewSource.GetDefaultView(ListBox.ItemsSource);
DataRowView row = (DataRowView) cv.AddNew();
row["Col1"] = value1;
row["Col2"] = value2;
cv.CommitNew();
cv.SortDescriptions.Clear();
cv.SortDescriptions.Add(new SortDescription("Col1", ListSortDirection.Descending));
cv.SortDescriptions.Add(new SortDescription("Col2", ListSortDirection.Ascending));
ListBox.SelectedItem = row;
ListBox.ScrollIntoView(row);