我使用的是wpftoolkit datagrid控件,该控件使用集合视图源进行记录分组。每当用户试图清除表单时,我也需要清除数据网格。我尝试将datagrid itemsource设置为null,但对于试图向未加载的datagrid添加任何记录的清晰功能.if用户来说,它工作得很好。
所以谁能给我提供一个清除数据网格的解决方案。
提前谢谢。
发布于 2011-02-03 01:28:19
我在这种情况下使用ObservableCollection。示例:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.items = new ObservableCollection<Item>(
Enumerable.Range(0, 10).Select(i => new Item {Id = i, Title = "str " + i}));
this.viewSource = new CollectionViewSource() { Source = this.items };
dataGrid1.ItemsSource = this.viewSource.View;
}
private ObservableCollection<Item> items;
private CollectionViewSource viewSource;
private void Button_Click(object sender, RoutedEventArgs e)
{
items.Clear();
//or
//((ObservableCollection<Item>)viewSource.Source).Clear();
}
public class Item
{
public int Id { get; set; }
public string Title { get; set; }
}
}Xaml:
<Button HorizontalAlignment="Center" Content="Clear" Click="Button_Click"/>
<DataGrid AutoGenerateColumns="True" Name="dataGrid1" />https://stackoverflow.com/questions/4873748
复制相似问题