xmlns:local="clr-namespace:BindableGraphics">
<UserControl.Resources>
<esri:SimpleRenderer x:Key="MyBlueRenderer">
<esri:SimpleRenderer.Symbol>
<esri:SimpleMarkerSymbol Color="Blue" Size="10" Style="Circle"/>
</esri:SimpleRenderer.Symbol>
</esri:SimpleRenderer>
<local:GraphicsSource x:Key="MyGraphicSource"/>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<esri:Map x:Name="MyMap">
<esri:ArcGISTiledMapServiceLayer Url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />
<esri:GraphicsLayer ID="MyGraphicsLayer" Graphics="{Binding Source={StaticResource MyGraphicSource}, Path=Graphics}" Renderer="{StaticResource MyBlueRenderer}"/>
</esri:Map>
</Grid>
Graphics="{Binding Source={StaticResource MappingSource}, Path=MappingItems, Mode=TwoWay, Converter={StaticResource mappingsConverter}}"
Binding binding = new Binding("Selected");
binding.Source = mappingItem;
binding.Mode = BindingMode.TwoWay;
BindingOperations.SetBinding(graphic, Graphic.SelectedProperty, binding);
<UserControl x:Class="ArcGISSilverlightSDK.UsingGraphicsSource"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:esri="http://schemas.esri.com/arcgis/client/2009"
xmlns:local="clr-namespace:ArcGISSilverlightSDK" >
<Grid x:Name="LayoutRoot" Background="White">
<Grid.Resources>
<local:Customers x:Key="customers" />
</Grid.Resources>
<esri:Map x:Name="MyMap" MouseClick="MyMap_OnMouseClick">
<esri:ArcGISTiledMapServiceLayer
Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer" />
<esri:GraphicsLayer ID="MyGraphicsLayer"
GraphicsSource="{Binding Source={StaticResource customers}, Path=MyGraphicsList}" />
</esri:Map>
</Grid>
</UserControl>using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Symbols;
using System;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Media;
namespace ArcGISSilverlightSDK
{
public partial class UsingGraphicsSource : UserControl
{
public UsingGraphicsSource()
{
InitializeComponent();
}
private void MyMap_OnMouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
{
var customer = LayoutRoot.Resources["customers"] as Customers;
customer.MyGraphicsList = Customers.CreateNewGraphicCollection();
}
}
public class Customers : INotifyPropertyChanged
{
private static readonly ESRI.ArcGIS.Client.Projection.WebMercator mercator = new ESRI.ArcGIS.Client.Projection.WebMercator();
public Customers()
{
MyGraphicsList= CreateNewGraphicCollection();
}
private GraphicCollection _myGraphicsList;
public GraphicCollection MyGraphicsList
{
get { return _myGraphicsList; }
set
{
_myGraphicsList = value;
RaisePropertyChanged("MyGraphicsList");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string prop)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
internal static GraphicCollection CreateNewGraphicCollection()
{
var graphicCollection = new GraphicCollection();
var random = new Random();
for (int i = 0; i < 10; i++)
{
var g = new Graphic
{
Geometry = mercator.FromGeographic(new MapPoint(random.Next(-180, 180), random.Next(-90, 90))),
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 = 28
}
};
graphicCollection.Add(g);
}
return graphicCollection;
}
}
}
namespace SIAW_Geospatial.ViewModels
{
public class Map_ViewModel : Base_ViewModel
{
private static ESRI.ArcGIS.Client.Projection.WebMercator mercator =
new ESRI.ArcGIS.Client.Projection.WebMercator();
private Map_Model mapModel;
public Map_ViewModel()
{
//models
mapModel = new Map_Model(this);
updateMapCmd = new RelayCommand(updateMap) { IsEnabled = true };
mapModel.getAirportData(); //populate lists
}
private ObservableCollection<AirportDataServiceReference.July2010RegScheduledFlightDelaysByOrigin> filteredAirportData;
public ObservableCollection<AirportDataServiceReference.July2010RegScheduledFlightDelaysByOrigin> FilteredAirportData
{
get { return filteredAirportData; }
set
{
filteredAirportData = value;
RaisePropertyChanged("FilteredAirportData");
//set the map
setGraphicsList();
}
}
//private GraphicCollection myGraphicsList;
//public GraphicCollection MyGraphicsList
private ObservableCollection<Graphic> myGraphicsList;
public ObservableCollection<Graphic> MyGraphicsList
{
get { return myGraphicsList; }
set
{
myGraphicsList = value;
RaisePropertyChanged("MyGraphicsList");
}
}
private readonly ICommand updateMapCmd;
public ICommand UpdateMapCmd
{
get
{
return updateMapCmd;
}
}
private void updateMap()
{
var query = from c in mapModel.AirportData
select c;
# region filters
if (AirlineListSelectedIndex > 0) //0 is All
{
string airline = AirlineList.ElementAt(AirlineListSelectedIndex);
query = query.Where(c => c.Airline.Equals(AirlineList.ElementAt(AirlineListSelectedIndex)));
}
if (DestinationListSelectedIndex > 0) //0 is All
{
query = query.Where(c => c.Destination.Equals(DestinationList.ElementAt(DestinationListSelectedIndex)));
}
if (ScheduledOperatorSelectedIndex > 0) //0 is No selection (i.e. don't filter by this)
{
if (ScheduledOperatorSelectedIndex == (int)(OperatorTypes.GreaterThan))
query = query.Where(c => c.Operations.Value > NumScheduledFlights);
else if (ScheduledOperatorSelectedIndex == (int)(OperatorTypes.EqualTo))
query = query.Where(c => c.Operations.Value.Equals(NumScheduledFlights));
else if (ScheduledOperatorSelectedIndex == (int)(OperatorTypes.LessThan))
query = query.Where(c => c.Operations.Value < NumScheduledFlights);
}
if (CancelledOperatorSelectedIndex > 0) //0 is No selection (i.e. don't filter by this)
{
if (CancelledOperatorSelectedIndex == (int)(OperatorTypes.GreaterThan))
query = query.Where(c => c.No_Canceled.Value > NumCancelledFlights);
else if (CancelledOperatorSelectedIndex == (int)(OperatorTypes.EqualTo))
query = query.Where(c => c.No_Canceled.Value.Equals(NumCancelledFlights));
else if (CancelledOperatorSelectedIndex == (int)(OperatorTypes.LessThan))
query = query.Where(c => c.No_Canceled.Value < NumCancelledFlights);
}
# endregion filters
//convert to an observable collection
ObservableCollection<AirportDataServiceReference.July2010RegScheduledFlightDelaysByOrigin> tmp = new ObservableCollection<AirportDataServiceReference.July2010RegScheduledFlightDelaysByOrigin>();
foreach (AirportDataServiceReference.July2010RegScheduledFlightDelaysByOrigin data in query)
{
tmp.Add(data);
}
FilteredAirportData = tmp;
}
public void setGraphicsList()
{
//GraphicCollection glist = new GraphicCollection();
ObservableCollection<Graphic> glist = new ObservableCollection<Graphic>();
SpatialReference sref = new SpatialReference(4326);
SolidColorBrush pointColor;
foreach (AirportDataServiceReference.July2010RegScheduledFlightDelaysByOrigin data in FilteredAirportData)
{
//get the right color based on the percent of cancelled flights
if (data.Percent_Canceled < 25)
pointColor = new SolidColorBrush(Colors.Green);
else if ((25 <= data.Percent_Canceled) && (data.Percent_Canceled < 75))
pointColor = new SolidColorBrush(Colors.Yellow);
else
pointColor = new SolidColorBrush(Colors.Red);
Graphic g = new Graphic()
{
Geometry = new MapPoint((double)data.Longitude, (double)data.Latitude, sref)
};
g.Symbol = new SimpleMarkerSymbol()
{
Color = pointColor,
Size = 18,
Style = SimpleMarkerSymbol.SimpleMarkerStyle.Circle
};
//mouse over data
g.Attributes.Add("OriginCity", "Origin City: " + data.City);
g.Attributes.Add("Destination", "Destination Airport: " + data.Destination);
g.Attributes.Add("PercentCanceled", "Percent of flights Canceled: " + data.Percent_Canceled);
//g.Attributes.Add("Lat", "Lat: " + data.Latitude);
//g.Attributes.Add("Lon", "Lon: " + data.Longitude);
glist.Add(g);
}
MyGraphicsList = new ObservableCollection<Graphic>(glist);
}
}
}namespace SIAW_Geospatial.Models
{
public class Map_Model : Base_Model
{
private AirportDataServiceReference.AirportDataServiceClient airportDataServiceClient;
private ViewModels.Map_ViewModel map_vm;
public Map_Model(ViewModels.Map_ViewModel vm)
{
map_vm = vm;
airportDataServiceClient = new AirportDataServiceReference.AirportDataServiceClient();
airportDataServiceClient.GetAirportDataCompleted += new EventHandler<AirportDataServiceReference.GetAirportDataCompletedEventArgs>(airportDataServiceClient_GetAirportDataCompleted);
}
void airportDataServiceClient_GetAirportDataCompleted(object sender, AirportDataServiceReference.GetAirportDataCompletedEventArgs e)
{
//set the combo boxes
AirportData = e.Result;
}
public void getAirportData()
{
airportDataServiceClient.GetAirportDataAsync();
}
private ObservableCollection<AirportDataServiceReference.July2010RegScheduledFlightDelaysByOrigin> airportData;
public ObservableCollection<AirportDataServiceReference.July2010RegScheduledFlightDelaysByOrigin> AirportData
{
get { return airportData; }
set
{
airportData = value;
RaisePropertyChanged("AirportData");
//to start, set the filtered data to the whole set
map_vm.FilteredAirportData = airportData;
}
}
}
}