This sample demonstrates how to create a graphics layer by binding to a source of items (i.e. graphics). The GraphicsSource property should reference an IEnumerable of Graphic objects (such as ObservableCollection). The appropriate event mechanisms are in place so changes in the items are reflected in the graphics layer.
using System;
using System.Collections.ObjectModel;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Symbols;
namespace ArcGISWPFSDK
{
publicpartialclass UsingGraphicsSource : UserControl
{
public UsingGraphicsSource()
{
InitializeComponent();
}
}
publicclass Customers : ObservableCollection<Graphic>
{
Random random;
privatestatic ESRI.ArcGIS.Client.Projection.WebMercator mercator =
new ESRI.ArcGIS.Client.Projection.WebMercator();
public Customers()
{
DispatcherTimer timer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(1) };
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
ClearItems();
random = new Random();
for (int i = 0; i < 10; i++)
{
Graphic g = new Graphic()
{
Geometry = new MapPoint(random.Next(-20000000, 20000000), random.Next(-20000000, 20000000))
};
g.Symbol = new SimpleMarkerSymbol()
{
Color = new SolidColorBrush(Color.FromArgb(255, (byte)random.Next(0, 255), (byte)random.Next(0, 255), (byte)random.Next(0, 255))),
Size = 24
};
Add(g);
}
}
}
}
Imports System.Collections.ObjectModel
Imports System.Windows.Controls
Imports System.Windows.Media
Imports System.Windows.Threading
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Geometry
Imports ESRI.ArcGIS.Client.Symbols
Namespace ArcGISWPFSDKVB
PartialPublicClass UsingGraphicsSource
Inherits UserControl
PublicSubNew()
InitializeComponent()
EndSubEndClassPublicClass Customers
Inherits ObservableCollection(Of Graphic)
Private random As Random
PrivateShared mercator AsNew ESRI.ArcGIS.Client.Projection.WebMercator()
PublicSubNew()
Dim timer AsNew DispatcherTimer() With { _
.Interval = TimeSpan.FromSeconds(4) _
}
AddHandler timer.Tick, New EventHandler(AddressOf timer_Tick)
timer.Start()
EndSubPrivateSub timer_Tick(sender AsObject, e As EventArgs)
ClearItems()
random = New Random()
For i AsInteger = 0 To 9
Dim g AsNew Graphic() With { _
.Geometry = New MapPoint(random.[Next](-20000000, 20000000), random.[Next](-20000000, 20000000)) _
}
g.Symbol = New SimpleMarkerSymbol() With { _
.Color = New SolidColorBrush(Color.FromArgb(255, CByte(random.[Next](0, 255)), CByte(random.[Next](0, 255)), CByte(random.[Next](0, 255)))), _
.Size = 24 _
}
Add(g)
NextEndSubEndClassEndNamespace