Support a ListCollectionView for a GraphicsLayer GraphicSource (WPF/.NET)

2804
2
Jump to solution
11-12-2014 07:58 AM
Labels (1)
EricPaitz
Esri Contributor

The WPF ListBox supports filtering by using a ListCollectionView or an ICollectionView and defining the Filter Predicate of the View. This allows several views to be created against the same source collection. The same functionality needs to be supported with the GraphicsLayer and its GraphicSource.

Something like the following.

Graphics = new ObservableCollection<Graphic>();
GraphicsView = new ListCollectionView(Graphics);
GraphicsView.Filter = new Predicate<object>(GraphicsViewFilter);

private bool GraphicsViewFilter(object item){
   return true;  //Custom filter logic goes her
}
Binding binding = new Binding("GraphicsView") { Source = ViewModel };
BindingOperations.SetBinding(graphicsLayer, GraphicsLayer.GraphicsSourceProperty, binding);


This would allow many GraphicsLayers to be created against a single source Collection but display different results.

See more at: http://ideas.arcgis.com/ideaView?id=087E00000005HKC&returnUrl=%2Fapex%2FideaList%3Fc%3D09a300000004x...

0 Kudos
1 Solution

Accepted Solutions
DominiqueBroux
Esri Frequent Contributor

GraphicsSource can be any enumerable of Graphics so you can create your own class that implements IEnumerable<Graphic> and subclasses CollectionView.

Something like:

public class GraphicsCollectionView : ListCollectionView, IEnumerable<Graphic>
{
   public GraphicsCollectionView(ObservableCollection<Graphic> graphics) : base(graphics)
   {
   }

   public new IEnumerator<Graphic> GetEnumerator()
   {
     return this.OfType<Graphic>().GetEnumerator();
   }
}

Then you can use it as you proposed:

Graphics = new ObservableCollection<Graphic>();
var graphicsView = new GraphicsCollectionView(Graphics);
graphicsView.Filter = new Predicate<object>(GraphicsViewFilter);

graphicsLayer.GraphicsSource = graphicsView;

private bool GraphicsViewFilter(object item){
   return true;  //Custom filter logic goes her
}

View solution in original post

0 Kudos
2 Replies
DominiqueBroux
Esri Frequent Contributor

GraphicsSource can be any enumerable of Graphics so you can create your own class that implements IEnumerable<Graphic> and subclasses CollectionView.

Something like:

public class GraphicsCollectionView : ListCollectionView, IEnumerable<Graphic>
{
   public GraphicsCollectionView(ObservableCollection<Graphic> graphics) : base(graphics)
   {
   }

   public new IEnumerator<Graphic> GetEnumerator()
   {
     return this.OfType<Graphic>().GetEnumerator();
   }
}

Then you can use it as you proposed:

Graphics = new ObservableCollection<Graphic>();
var graphicsView = new GraphicsCollectionView(Graphics);
graphicsView.Filter = new Predicate<object>(GraphicsViewFilter);

graphicsLayer.GraphicsSource = graphicsView;

private bool GraphicsViewFilter(object item){
   return true;  //Custom filter logic goes her
}

0 Kudos
EricPaitz
Esri Contributor

Hey Dominique, thank you very much for this explination. I was able to get this to work in a test project. Very cool! 🙂

0 Kudos