2011-02-02 38 views
0

我正在使用wpftoolkit datagrid控件,該控件使用collectionviewsource進行記錄分組綁定。每當用戶嘗試清除表單時,我也需要清除數據網格。 我試圖將datagrid itemsource設置爲null,但它對於清晰的功能工作正常。如果用戶試圖將任何記錄添加到datagrid,它不會加載。WpfDatagrid Collectionviewsource clear

所以任何人都可以給我提供一個清除數據網格的解決方案。

在此先感謝。

回答

0

我對這種情況使用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" />