1

我可以知道,我怎样才能在这个默认值上执行搜索DataGrid?同时增加了它的价值。

<DataGrid Name="table" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" IsTextSearchEnabled="True" Background="White">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Timestamp" Binding="{Binding StartDate}" SortDirection="Descending" SortMemberPath="StartDate" IsReadOnly="True" />
        <DataGridTextColumn Header="Title" Binding="{Binding Title}" IsReadOnly="True" />
        <DataGridTextColumn Header="Description" Binding="{Binding Description}" IsReadOnly="True" />
        <DataGridTextColumn Header="Type" Binding="{Binding Tag}" IsReadOnly="True" />
    </DataGrid.Columns>
</DataGrid>
4

1 回答 1

2

如何创建和使用 CollectionView

下面的例子展示了如何创建一个集合视图并将它绑定到一个 ListBox以同样的方式你可以将它与 datagrid 一起使用

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ListBox ItemsSource={Binding Customers} />
</Window>

public class CustomerView
{
   public CustomerView()
   {
        DataContext = new CustomerViewModel();
   }
}


public class CustomerViewModel
{
    private ICollectionView _customerView;

    public ICollectionView Customers
    {
        get { return _customerView; }
    }

    public CustomerViewModel()
    {
        IList<Customer> customers = GetCustomers();
        _customerView = CollectionViewSource.GetDefaultView(customers);
    }
}

过滤

要过滤集合视图,您可以定义一个回调方法来确定该项目是否应该是视图的一部分。该方法应具有以下签名:bool Filter(object item)。现在将该方法的委托设置为 CollectionView 的 Filter 属性,您就完成了。

ICollectionView _customerView = CollectionViewSource.GetDefaultView(customers);
_customerView.Filter = CustomerFilter

private bool CustomerFilter(object item)
{
    Customer customer = item as Customer;
    return customer.Name.Contains( _filterString );
}

刷新过滤器

如果您更改过滤条件并且想要刷新视图,则必须在集合视图上调用 Refresh()

public string FilterString
{
  get { return _filterString; }
  set 
  { 
      _filterString = value; 
      NotifyPropertyChanged("FilterString");
      _customerView.Refresh();
  }
}
于 2013-10-01T10:33:39.610 回答