Ce sujet n'a pas encore été évalué - Évaluez ce sujet

CollectionView.Filter, propriété

Gets or sets a callback used to determine if an item is suitable for inclusion in the view.

Espace de noms: System.Windows.Data
Assembly : PresentationFramework (dans presentationframework.dll)
Espace de noms XML :  http://schemas.microsoft.com/winfx/2006/xaml/presentation

public virtual Predicate<Object> Filter { get; set; }
/** @property */
public Predicate<Object> get_Filter ()

/** @property */
public void set_Filter (Predicate<Object> value)

public function get Filter () : Predicate<Object>

public function set Filter (value : Predicate<Object>)

Vous ne pouvez pas utiliser cette propriété dans XAML.

Valeur de la propriété

A method used to determine if an item is suitable for inclusion in the view.
Type d'exceptionCondition

NotSupportedException

If the current implementation does not support filtering.

Simpler implementations do not support filtering and will throw a NotSupportedException. Use the CanFilter property to test if filtering is supported before assigning this property with a non-null value.

This example shows how to filter data in a view.

To create a filter, define a method that provides the filtering logic. The method is used as a callback and accepts a parameter of type object. The following method returns all the Order objects with the filled property set to "No", filtering out the rest of the objects.

public bool Contains(object de)
{
    Order order = de as Order;
    //Return members whose Orders have not been filled
    return(order.Filled== "No");
}

You can then apply the filter, as shown in the following example. In this example, myCollectionView is a ListCollectionView object.

myCollectionView.Filter = new Predicate<object>(Contains);

To undo filtering, you can set the Filter property to référence Null (Nothing en Visual Basic):

myCollectionView.Filter = null;

For information about how to create or obtain a view, see How to: Create a View of a Data Collection. For the complete example, see Sorting and Filtering Items in a View Sample.

If your view object comes from a CollectionViewSource object, you apply filtering logic by setting an event handler for the Filter event. In the following example, listingDataView is an instance of CollectionViewSource.

listingDataView.Filter += new FilterEventHandler(ShowOnlyBargainsFilter);

The following shows the implementation of the example ShowOnlyBargainsFilter filter event handler. This event handler uses the Accepted property to filter out AuctionItem objects that have a CurrentPrice of $25 or greater.

private void ShowOnlyBargainsFilter(object sender, FilterEventArgs e)
{
    AuctionItem product = e.Item as AuctionItem;
    if (product != null)
    {
        // Filter out products with price 25 or above
        if (product.CurrentPrice < 25)
        {
            e.Accepted = true;
        }
        else
        {
            e.Accepted = false;
        }
    }
}

For the complete example, see Data Binding Demo.

Microsoft .NET Framework 3.0 est pris en charge sur Windows Vista, Microsoft Windows XP SP2 et Windows Server 2003 SP1.

.NET Framework

Prise en charge dans : 3.0
Cela vous a-t-il été utile ?
(1500 caractères restants)

Ajouts de la communauté

AJOUTER
Microsoft réalise une enquête en ligne pour recueillir votre opinion sur le site Web de MSDN. Si vous choisissez d’y participer, cette enquête en ligne vous sera présentée lorsque vous quitterez le site Web de MSDN.

Si vous souhaitez y participer,
© 2013 Microsoft. Tous droits réservés.